【问题标题】:Asp Core API. Policy RequireAuthenticatedUser does not allow authenticated user after publishing on LinuxASP 核心 API。策略 RequireAuthenticatedUser 在 Linux 上发布后不允许经过身份验证的用户
【发布时间】:2019-12-11 15:57:49
【问题描述】:

我正在使用 JWT 令牌和策略:

services.AddAuthorization(options => {
                options.AddPolicy("AllAuthenticated", policy => policy.RequireAuthenticatedUser());
            });

这适用于本地 Windows 和 Mac 开发环境:

[HttpPost("log-out")]
        [Authorize(Policy = "AllAuthenticated")]
        public async Task<IActionResult> LogOut(){}

登录用户可以使用 [Authorize(Policy = "AllAuthenticated")] 访问端点。未登录的用户不能。

但是在发布到 Linux 之后: 用户仍然可以登录,但不能点击 [Authorize(Policy = "AllAuthenticated")] 修饰的操作:返回 401 Unauthorized。

登录时用户收到 JWT 令牌,该令牌在请求标头中的每个下一个请求中发送回后端:授权:承载 [token here]。

我检查了令牌与从后端核心发送的完全一样。

这种行为出现在 2 台 Linux 机器上,所以我猜这可能与发布到 Linux 环境有关:

dotnet publish -c Release -o /home/[user folder]/[publishing folder]

令牌创建:

var tokeOptions = new JwtSecurityToken(
                issuer: "mysite.com",
                audience: "myClientApp",
                claims: claims,
                expires: DateTime.Now.AddMinutes(KEY_EXPIRATION_TIME),
                signingCredentials: signinCredentials
            );
            var tokenString = new JwtSecurityTokenHandler().WriteToken(tokeOptions);
            return tokenString;

至少有人能指出我如何在这些 Linux 机器上调试问题吗?

【问题讨论】:

    标签: c# asp.net-core asp.net-identity


    【解决方案1】:

    问题出在政策上。添加 JWT AuthenticationScheme 解决了这个问题。

    之前:

    services.AddAuthorization(options => {               
    options.AddPolicy("AllAuthenticated", policy => policy.RequireAuthenticatedUser()); });
    

    之后:

    services.AddAuthorization(options => {               
         options.AddPolicy("AllAuthenticated", 
            policy => policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme).
                    RequireAuthenticatedUser());});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-30
      • 2017-07-08
      • 2015-05-01
      • 2020-01-01
      • 2018-04-06
      • 1970-01-01
      • 2017-01-04
      • 2018-02-20
      相关资源
      最近更新 更多