public async Task<IActionResult> Auth([FromHeader] string Authorization)
{
//Получаем claims из BasicAuthentication токена
var user = GetClaims(Authorization);
var claims = new List<Claim>() { new Claim(ClaimTypes.Name, user.Username) };
var utcNow = DateTime.UtcNow;
var jwt = new JwtSecurityToken(
issuer: AuthOptions.ISSUER,
notBefore: utcNow,
claims: claims,
expires: utcNow.AddHours(AuthOptions.TokenLifetime),
signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(),
SecurityAlgorithms.HmacSha256));
var jwtToken = new JwtSecurityTokenHandler().WriteToken(jwt);
return Ok(jwtToken);
}
private static IServiceCollection AddJwtBearerAuthentication(this IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = true;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = AuthOptions.ISSUER,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
ValidateAudience = false,
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
ValidateIssuerSigningKey = true,
};
});
return services;
}
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
сразу же отдает 401 Unauthorized. В чем причина? Время жизни токена 6 часов