From 5b8d8b9e552440b5534c8a727979038fc17cc7d9 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Thu, 5 Sep 2024 10:24:00 +0200 Subject: [PATCH] refactor(api): Aktualisierung von ControllerExtensions zur Nutzung von ClaimsPrincipal anstelle von ControllerBase MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Refactoring der Erweiterungsmethoden, sodass sie auf `ClaimsPrincipal` anstatt auf `ControllerBase` basieren. - Alle Verweise und Verwendungen dieser Erweiterungsmethoden im gesamten Code aktualisiert. - Dies verbessert die Flexibilität und entkoppelt das Abrufen von Benutzeransprüchen von den Controllern. --- .../Controllers/ControllerExtensions.cs | 20 +++++++++---------- .../Controllers/EnvelopeController.cs | 2 +- .../Controllers/EnvelopeReceiverController.cs | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs index a897824b..1121a9b8 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/ControllerExtensions.cs @@ -5,20 +5,20 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers { public static class ControllerExtensions { - public static int? GetId(this ControllerBase controller) - => int.TryParse(controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, out int result) + public static int? GetId(this ClaimsPrincipal user) + => int.TryParse(user.FindFirst(ClaimTypes.NameIdentifier)?.Value, out int result) ? result : null; - public static string? GetUsername(this ControllerBase controller) - => controller.User.FindFirst(ClaimTypes.Name)?.Value; + public static string? GetUsername(this ClaimsPrincipal user) + => user.FindFirst(ClaimTypes.Name)?.Value; - public static string? GetName(this ControllerBase controller) - => controller.User.FindFirst(ClaimTypes.Surname)?.Value; + public static string? GetName(this ClaimsPrincipal user) + => user.FindFirst(ClaimTypes.Surname)?.Value; - public static string? GetPrename(this ControllerBase controller) - => controller.User.FindFirst(ClaimTypes.GivenName)?.Value; + public static string? GetPrename(this ClaimsPrincipal user) + => user.FindFirst(ClaimTypes.GivenName)?.Value; - public static string? GetEmail(this ControllerBase controller) - => controller.User.FindFirst(ClaimTypes.Email)?.Value; + public static string? GetEmail(this ClaimsPrincipal user) + => user.FindFirst(ClaimTypes.Email)?.Value; } } \ No newline at end of file diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeController.cs index 1c3dcd14..652f7f08 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeController.cs @@ -24,7 +24,7 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers { try { - var id = this.GetId(); + var id = User.GetId(); if (id is int intId) return await _envelopeService.ReadByIdAsync(intId).ThenAsync( diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs index faa85852..d10194d9 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/EnvelopeReceiverController.cs @@ -24,12 +24,12 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers { try { - var username = this.GetUsername(); + var username = User.GetUsername(); if (username is null) { _logger.LogError(@"Envelope Receiver dto cannot be sent because username claim is null. Potential authentication and authorization error. The value of other claims are [id: {id}], [username: {username}], [name: {name}], [prename: {prename}], [email: {email}].", - this.GetId(), this.GetUsername(), this.GetName(), this.GetPrename(), this.GetEmail()); + User.GetId(), User.GetUsername(), User.GetName(), User.GetPrename(), User.GetEmail()); return StatusCode(StatusCodes.Status500InternalServerError); }