From 730b218eb5530a7c0bda41062ed14f5b740cd894 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 25 Oct 2024 14:41:06 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Erweiterungsmethode=20hinzugef=C3=BCgt,?= =?UTF-8?q?=20um=20UserReadDto=20in=20Claims-Liste=20zu=20konvertieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WorkFlow.API/Controllers/AuthController.cs | 12 +----------- WorkFlow.API/Models/ModelExtensions.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 WorkFlow.API/Models/ModelExtensions.cs diff --git a/WorkFlow.API/Controllers/AuthController.cs b/WorkFlow.API/Controllers/AuthController.cs index a10b81d..9a2a16a 100644 --- a/WorkFlow.API/Controllers/AuthController.cs +++ b/WorkFlow.API/Controllers/AuthController.cs @@ -81,18 +81,8 @@ namespace WorkFlow.API.Controllers UserReadDto user = uRes.Data; - // Create claims - var claims = new List - { - new (ClaimTypes.NameIdentifier, user.Id.ToString()), - new (ClaimTypes.Name, user.Username), - new (ClaimTypes.Surname, user.Name ?? ""), - new (ClaimTypes.GivenName, user.Prename ?? ""), - new (ClaimTypes.Email, user.Email ?? "") - }; - // Create claimsIdentity - var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + var claimsIdentity = new ClaimsIdentity(user.ToClaimList(), CookieAuthenticationDefaults.AuthenticationScheme); // Create authProperties var authProperties = new AuthenticationProperties diff --git a/WorkFlow.API/Models/ModelExtensions.cs b/WorkFlow.API/Models/ModelExtensions.cs new file mode 100644 index 0000000..cbc67f7 --- /dev/null +++ b/WorkFlow.API/Models/ModelExtensions.cs @@ -0,0 +1,16 @@ +using DigitalData.UserManager.Application.DTOs.User; +using System.Security.Claims; + +namespace WorkFlow.API.Models +{ + public static class ModelExtensions + { + public static List ToClaimList(this UserReadDto user) => [ + new (ClaimTypes.NameIdentifier, user.Id.ToString()), + new (ClaimTypes.Name, user.Username), + new (ClaimTypes.Surname, user.Name ?? ""), + new (ClaimTypes.GivenName, user.Prename ?? ""), + new (ClaimTypes.Email, user.Email ?? "") + ]; + } +} \ No newline at end of file