问题描述
您好,我正在使用基于令牌的身份验证来开发应用程序。
在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);
}
我在哪里失踪?
预先感谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)