improve user claim retrieval

This commit is contained in:
cuqmbr 2025-06-09 14:17:10 +03:00
parent 29f0614e56
commit 8bed1d39f3
Signed by: cuqmbr
GPG Key ID: 0AA446880C766199

View File

@ -18,7 +18,9 @@ public sealed class AspNetSessionUserService : SessionUserService
get get
{ {
var claimValue = _httpContext.User.Claims var claimValue = _httpContext.User.Claims
.FirstOrDefault(c => c.Properties .FirstOrDefault(c =>
c.Type == JwtRegisteredClaimNames.Sub ||
c.Properties
.Any(p => p.Value == JwtRegisteredClaimNames.Sub)) .Any(p => p.Value == JwtRegisteredClaimNames.Sub))
?.Value; ?.Value;
@ -29,17 +31,23 @@ public sealed class AspNetSessionUserService : SessionUserService
} }
public string? Username => _httpContext.User.Claims public string? Username => _httpContext.User.Claims
.FirstOrDefault(c => c.Properties .FirstOrDefault(c =>
c.Type == JwtRegisteredClaimNames.Nickname ||
c.Properties
.Any(p => p.Value == JwtRegisteredClaimNames.Nickname)) .Any(p => p.Value == JwtRegisteredClaimNames.Nickname))
?.Value; ?.Value;
public string? Email => _httpContext.User.Claims public string? Email => _httpContext.User.Claims
.FirstOrDefault(c => c.Properties .FirstOrDefault(c =>
c.Type == JwtRegisteredClaimNames.Email ||
c.Properties
.Any(p => p.Value == JwtRegisteredClaimNames.Email)) .Any(p => p.Value == JwtRegisteredClaimNames.Email))
?.Value; ?.Value;
public ICollection<IdentityRole> Roles => _httpContext.User.Claims public ICollection<IdentityRole> Roles => _httpContext.User.Claims
.Where(c => c.Properties .Where(c =>
c.Type == "roles" ||
c.Properties
.Any(p => p.Value == "roles")) .Any(p => p.Value == "roles"))
.Select(c => IdentityRole.FromName(c.Value)) .Select(c => IdentityRole.FromName(c.Value))
.ToArray() ?? default!; .ToArray() ?? default!;