Init
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using maintenance.Authentication.Abstractions;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace maintenance.Authentication.Core;
|
||||
|
||||
public sealed class IdentityContextFactory : IIdentityContextFactory
|
||||
{
|
||||
private static readonly string[] GroupClaimTypes =
|
||||
{
|
||||
"groups",
|
||||
"group",
|
||||
ClaimTypes.GroupSid,
|
||||
"http://schemas.xmlsoap.org/claims/Group"
|
||||
};
|
||||
|
||||
public IdentityContext Create(ClaimsPrincipal principal)
|
||||
{
|
||||
var groups = principal.Claims
|
||||
.Where(c => GroupClaimTypes.Contains(c.Type, StringComparer.OrdinalIgnoreCase))
|
||||
.Select(c => c.Value)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var roles = principal.Claims
|
||||
.Where(c => c.Type == ClaimTypes.Role)
|
||||
.Select(c => c.Value)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
var userId = principal.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? principal.FindFirstValue("sub")
|
||||
?? principal.Identity?.Name
|
||||
?? "unknown";
|
||||
|
||||
var displayName = principal.FindFirstValue(ClaimTypes.Name)
|
||||
?? principal.Identity?.Name
|
||||
?? userId;
|
||||
|
||||
return new IdentityContext
|
||||
{
|
||||
UserId = userId,
|
||||
DisplayName = displayName,
|
||||
Groups = groups,
|
||||
Roles = roles
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user