.net core 2.1基于令牌的身份验证给出401错误

问题描述

您好,我正在使用基于令牌的身份验证来开发应用程序。

在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);
 }

此图像用于生成代码try

enter image description here

生成并返回后,我尝试了邮递员此令牌,例如

enter image description here

我在哪里失踪?

预先感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...