你好,我正在开发基于令牌身份验证的应用程序。
在startup.cs中添加了以下代码。
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(jwtBearerOptions =>
{
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters()
{
ValidateActor = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer ="Issuer",
ValidAudience ="Audience",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("ProEMLh5e_qnzdNUQrqdHPgp"))
};
});
和
app.UseAuthentication();
在控制器中
HttpPost("AdminLogin")]
public IActionResult AdminLogin(Admin admin)
{
var result = admine.Login(admin);
if (result!=null && result.Id>0)
{
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, admin.Username),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var token = new JwtSecurityToken
(
issuer: "issuer",
audience: "Audience",
claims: claims,
expires: DateTime.UtcNow.AddDays(1),
notBefore: DateTime.UtcNow,
signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes("ProEMLh5e_qnzdNUQrqdHPgp")),
SecurityAlgorithms.HmacSha256)
);
result.Token = new JwtSecurityTokenHandler().WriteToken(token);
}
return Ok(result);
}
它工作并生成令牌和返回。我的问题是,当我用这个令牌发送请求时。401授权错误。对于以下一个
[Authorize]
[HttpPost("Exams")]
public IActionResult Exams([FromBody] Exam model)
{
var result = exam.GEtExams(model.Id);
return Ok(result);
}
此图像用于生成代码尝试
在我生成并返回后,我尝试了邮递员这个令牌,就像
我的失踪在哪里?
提前感谢