【发布时间】:2018-12-22 09:25:25
【问题描述】:
我想延长 JWT 令牌的生命周期,但我不能。
我尝试用谷歌搜索这件事,发现对JwtBearerOptions.TokenValidationParameters.ClockSkew的引用。
我还尝试提供 1 分 20 秒的时间跨度,但应用并未考虑这些更改。
Startup.cs:
services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters()
{
ClockSkew = TimeSpan.FromSeconds(20),
RequireExpirationTime = true,
RequireSignedTokens = true,
ValidateIssuerSigningKey = true,
ValidateLifetime = true,
IssuerSigningKey = Configuration.GetSymmetricSecurityKey(),
ValidAudience = Configuration.GetValidAudience(),
ValidIssuer = Configuration.GetValidIssuer()
};
});
这是Authenticate 操作:
[AllowAnonymous]
[HttpPost]
public async Task<AuthenticateOutput> Authenticate([FromBody] AuthenticateInput input)
{
string subdomain = Request.GetSubDomain();
var user = await _userService.Authenticate(input.UserName, input.Password, subdomain);
if (user == null)
{
throw new Exception("Unauthorised");
}
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Issuer = _config.GetValidIssuer(),
Audience = _config.GetValidAudience(),
SigningCredentials = new SigningCredentials(_config.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256),
Subject = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, user.UserName),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
})
};
var token = tokenHandler.CreateToken(tokenDescriptor);
string tokenString = tokenHandler.WriteToken(token);
return new AuthenticateOutput() { UserId = user.Id, Token = tokenString };
}
我错过了什么吗?
【问题讨论】:
标签: angular authentication asp.net-core jwt