|
|
|
@ -59,27 +59,25 @@ public class UserContext<TId>(
|
|
|
|
|
|
|
|
|
|
public string Email
|
|
|
|
|
{
|
|
|
|
|
get => _email ??= principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Email).Value;
|
|
|
|
|
get => _email ??= GetClaimValue(JwtRegisteredClaimNames.Email);
|
|
|
|
|
set => _email = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] RoleIds
|
|
|
|
|
{
|
|
|
|
|
get => _roleIds ??= principal.Claims.Where(c => c.Type == ClaimConstants.RoleId).Select(c => c.Value).ToArray();
|
|
|
|
|
get => _roleIds ??= GetClaimValues(ClaimConstants.RoleId);
|
|
|
|
|
set => _roleIds = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] RoleNames
|
|
|
|
|
{
|
|
|
|
|
get => _roleNames ??= principal.Claims.Where(c => c.Type == ClaimTypes.Role)
|
|
|
|
|
.Select(c => c.Value).ToArray();
|
|
|
|
|
get => _roleNames ??= GetClaimValues(ClaimTypes.Role);
|
|
|
|
|
set => _roleNames = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string[] Permissions
|
|
|
|
|
{
|
|
|
|
|
get => _permissions ??= principal.Claims.Where(c => c.Type == ClaimConstants.PermissionCode)
|
|
|
|
|
.Select(c => c.Value).ToArray();
|
|
|
|
|
get => _permissions ??= GetClaimValues(ClaimConstants.PermissionCode);
|
|
|
|
|
set => _permissions = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -91,7 +89,7 @@ public class UserContext<TId>(
|
|
|
|
|
|
|
|
|
|
public JwtTokenInfo GenerateTokenInfo(
|
|
|
|
|
IList<Claim>? claims = null,
|
|
|
|
|
double duration = 0,
|
|
|
|
|
int duration = 0,
|
|
|
|
|
string schemeName = JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
{
|
|
|
|
|
claims ??= GetClaimsFromUserContext();
|
|
|
|
@ -104,9 +102,9 @@ public class UserContext<TId>(
|
|
|
|
|
{
|
|
|
|
|
Issuer = jwtContext.Issuer,
|
|
|
|
|
Audience = jwtContext.Audience,
|
|
|
|
|
Claims = claims?.ToDictionary(c => c.Type, c => (object)c.Value),
|
|
|
|
|
NotBefore = DateTime.Now,
|
|
|
|
|
Expires = DateTime.Now.AddSeconds(duration),
|
|
|
|
|
Subject = new ClaimsIdentity(claims),
|
|
|
|
|
NotBefore = DateTime.UtcNow,
|
|
|
|
|
Expires = DateTime.UtcNow.AddSeconds(duration),
|
|
|
|
|
SigningCredentials = jwtContext.SigningCredentials,
|
|
|
|
|
};
|
|
|
|
|
var token = jsonWebTokenHandler.CreateToken(tokenDescriptor);
|
|
|
|
@ -123,7 +121,7 @@ public class UserContext<TId>(
|
|
|
|
|
new(JwtRegisteredClaimNames.Name, Name),
|
|
|
|
|
new(JwtRegisteredClaimNames.Email, Email),
|
|
|
|
|
new(JwtRegisteredClaimNames.Iat,
|
|
|
|
|
EpochTime.GetIntDate(DateTime.Now).ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
EpochTime.GetIntDate(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
ClaimValueTypes.Integer64),
|
|
|
|
|
new(JwtRegisteredClaimNames.Exp,
|
|
|
|
|
TimeSpan.FromSeconds(jwtContext.Duration).ToString())
|
|
|
|
|