|
|
|
@ -28,20 +28,22 @@ public class UserContext<TId>(
|
|
|
|
|
|
|
|
|
|
public string RemoteIpAddress => httpContextAccessor.HttpContext?.GetRequestIp()!;
|
|
|
|
|
|
|
|
|
|
public JwtTokenInfo GenerateTokenInfo()
|
|
|
|
|
public JwtTokenInfo GenerateTokenInfo(
|
|
|
|
|
JwtSecurityToken? securityToken = null,
|
|
|
|
|
double? duration = null,
|
|
|
|
|
string schemeName = JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
{
|
|
|
|
|
var claims = GetClaimsFromUserContext();
|
|
|
|
|
var jwtToken = new JwtSecurityToken(
|
|
|
|
|
securityToken ??= new JwtSecurityToken(
|
|
|
|
|
issuer: jwtOptions.Issuer,
|
|
|
|
|
audience: jwtOptions.Audience,
|
|
|
|
|
claims: claims,
|
|
|
|
|
notBefore: DateTime.Now,
|
|
|
|
|
expires: DateTime.Now.AddSeconds(jwtOptions.Expiration),
|
|
|
|
|
expires: DateTime.Now.AddSeconds(jwtOptions.Duration),
|
|
|
|
|
signingCredentials: jwtOptions.SigningCredentials);
|
|
|
|
|
var token = jwtSecurityTokenHandler.WriteToken(jwtToken);
|
|
|
|
|
var token = jwtSecurityTokenHandler.WriteToken(securityToken);
|
|
|
|
|
token = encryptionService.Encrypt(token);
|
|
|
|
|
return new JwtTokenInfo(token, jwtOptions.Expiration,
|
|
|
|
|
JwtBearerDefaults.AuthenticationScheme);
|
|
|
|
|
return new JwtTokenInfo(token, duration ?? jwtOptions.Duration, schemeName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<Claim> GetClaimsFromUserContext()
|
|
|
|
@ -55,7 +57,8 @@ public class UserContext<TId>(
|
|
|
|
|
new(JwtRegisteredClaimNames.Iat,
|
|
|
|
|
EpochTime.GetIntDate(DateTime.Now).ToString(CultureInfo.InvariantCulture),
|
|
|
|
|
ClaimValueTypes.Integer64),
|
|
|
|
|
new(JwtRegisteredClaimNames.Exp, jwtOptions.Expiration.ToString())
|
|
|
|
|
new(JwtRegisteredClaimNames.Exp,
|
|
|
|
|
TimeSpan.FromSeconds(jwtOptions.Duration).ToString())
|
|
|
|
|
};
|
|
|
|
|
claims.AddRange(RoleIds.Select(rId => new Claim(ClaimTypes.Role, rId)));
|
|
|
|
|
return claims;
|
|
|
|
|