【问题标题】:JWT - Always unauthorizedJWT - 总是未经授权
【发布时间】:2021-06-08 12:17:14
【问题描述】:

我有一个非常简单的用户登录代码如下:

        [HttpPost]
        [Route("login")]
        public async Task<IActionResult> Login([FromBody] LoginModel model)
        {
            var user = await userManager.FindByEmailAsync(model.Email);
            if (user != null && await userManager.CheckPasswordAsync(user, model.Password))
            {
                var userRoles = await userManager.GetRolesAsync(user);

                var authClaims = new List<Claim>
                {
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                };

                foreach (var userRole in userRoles)
                {
                    authClaims.Add(new Claim(ClaimTypes.Role, userRole));
                }

                var authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:Secret"]));

                var token = new JwtSecurityToken(
                    issuer: _configuration["JWT:ValidIssuer"],
                    audience: _configuration["JWT:ValidAudience"],
                    expires: DateTime.Now.AddHours(3),
                    claims: authClaims,
                    signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256)
                    );

                return Ok(new
                {
                    token = new JwtSecurityTokenHandler().WriteToken(token),
                    expiration = token.ValidTo
                });
            }
            return Unauthorized();
        }

这会按预期向我发送令牌

当我使用令牌向简单控制器发送请求时,我得到 401-Unauthorized

[Authorize]
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet]
    [Route("getme")]
    public async Task<IActionResult> Get()
    {
        return Ok("Good");
    }
}

这是我的 Startup.cs

    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnStr")));

    // For Identity
    services.AddIdentity<ApplicationUser, IdentityRole>(o =>
    {
        // configure identity options
        o.Password.RequireDigit = false;
        o.Password.RequireLowercase = false;
        o.Password.RequireUppercase = false;
        o.Password.RequireNonAlphanumeric = false;
        o.Password.RequiredLength = 6;
    })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();

    // Adding Authentication
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    })

    // Adding Jwt Bearer
    .AddJwtBearer(options =>
    {
        options.SaveToken = true;
        options.RequireHttpsMetadata = false;
        options.TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = true,
            ValidateAudience = true,
            ValidAudience = Configuration["JWT:ValidAudience"],
            ValidIssuer = Configuration["JWT:ValidIssuer"],
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]))
        };
    });

以下是我的应用设置

我可以在https://jwt.io/ 验证令牌

由于某种原因,当我向TestController 发送请求时,它总是发回未经授权的响应。知道我做错了什么吗?

【问题讨论】:

  • 为什么要使用 对称 密钥进行签名?您是否看到任何异常或日志可以告诉您返回 401 的原因?
  • 你真的需要options.SaveToken吗?你试过没有那个吗?
  • 调试顺便说一句的一种简单方法是闯入OnAuthenticationFailed:options.Events = new JwtBearerEvents() { OnAuthenticationFailed = c =&gt; { return Task.CompletedTask; } };。如果令牌被认为无效,则应调用(但我认为如果令牌完全丢失,则不会调用它)。
  • @StephenCleary 我认为这是一个更安全的选择,但我对 JWT 还很陌生,所以请告知为什么不使用对称密钥进行签名 - >泄漏以解决我必须使用身份验证的问题 >按该顺序路由>授权,我的授权在导致问题的身份验证之前
  • @Ali:除了《应用密码学》这本书,别无他求。 JWT 有 3 部分,而不是“3 部分密钥”。

标签: asp.net-core asp.net-web-api jwt


【解决方案1】:

如果这对任何人有帮助,问题出在我的Startup 文件中的Configure 部分下,我在UseAuthorization 之后有UseAuthentication,这导致了问题。正确的顺序如下解决了问题

app.UseAuthentication(); 
app.UseRouting(); 
app.UseAuthorization();
app.UseHttpsRedirection();   

【讨论】:

  • 恕我直言,app.UseHttpsRedirection(); 应该是这里的第一个中间件。根据当前订单,可以通过未加密的渠道交换代币
猜你喜欢
  • 2018-04-08
  • 2018-05-19
  • 2017-01-12
  • 2018-02-02
  • 2018-03-18
  • 2018-06-06
  • 2021-09-21
  • 1970-01-01
  • 2020-03-28
相关资源
最近更新 更多