【发布时间】:2019-03-15 06:28:08
【问题描述】:
这是我的令牌解码器。当我尝试对其进行解码时,我的主体最终为空,从而导致此错误:
'IDX10208:无法验证受众。 validationParameters.ValidAudience 为 null 或空格,并且 validationParameters.ValidAudiences 为空。'
当我解码我的令牌进行检查时
“nbf”:1539167980,“exp”:1539168580,“iat”:1539167980,“iss”: "http://localhost:55260", "aud": "http://localhost:55260"
这也是我的令牌生成器运行的主机。为什么委托人会导致问题?
public class DecodeToken
{
private IConfiguration configuration;
public DecodeToken(IConfiguration configuration)
{
this.configuration = configuration;
}
public AuthenticationDto Decode(String Input)
{
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JwtAuthentication:SecurityKey"]));
var handler = new JwtSecurityTokenHandler();
var tokenSecure = handler.ReadToken(Input) as SecurityToken;
var validations = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = key,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = false
};
SecurityToken securityToken;
var principal = handler.ValidateToken(Input, validations, out securityToken);
var jwtSecurityToken = securityToken as JwtSecurityToken;
if (jwtSecurityToken == null || !jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase))
{
throw new SecurityTokenException("Invalid Token");
}
AuthenticationDto authenticationDto = new AuthenticationDto
{
Email = principal.Claims.Where(c => c.Type == "Email").Select(c => c.Value).SingleOrDefault(),
UserName = principal.Claims.Where(c => c.Type == "UserName").Select(c => c.Value).SingleOrDefault(),
FirstName = principal.Claims.Where(c => c.Type == "FirstName").Select(c => c.Value).SingleOrDefault(),
LastName = principal.Claims.Where(c => c.Type == "LastName").Select(c => c.Value).SingleOrDefault(),
PhoneNumber = principal.Claims.Where(c => c.Type == "PhoneNumber").Select(c => c.Value).SingleOrDefault(),
Id = principal.Claims.Where(c => c.Type == "Id").Select(c => c.Value).SingleOrDefault(),
ExpiryDateTime = principal.Claims.Where(c => c.Type == "exp").Select(c => c.Value).SingleOrDefault(),
Roles = principal.Claims.Where(c => c.Type == "Roles").Select(c => c.Value).ToList(),
};
return authenticationDto;
}
}
这就是我的 Startup.cs 的样子:
services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = "Jwt";
options.DefaultChallengeScheme = "Jwt";
})
.AddJwtBearer("Jwt", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtAuthentication:SecurityKey"])),
ValidateIssuer = true,
ValidIssuer = Configuration["JwtAuthentication:Issuer"],
ValidateAudience = true,
ValidAudience = Configuration["JwtAuthentication:Audience"],
ValidateLifetime = true, //validate the expiration and not before values in the token
ClockSkew = TimeSpan.Zero //5 minute tolerance for the expiration date
};
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
{
context.Response.Headers.Add("Token-Expired", "true");
}
return Task.CompletedTask;
}
};
});
我是不是配置有问题?
【问题讨论】:
标签: c# asp.net asp.net-core asp.net-core-mvc jwt