Asp核心API。策略 RequireAuthenticatedUser 在 Linux 上发布后不允许经过身份验证的用户

Asp Core API. Policy RequireAuthenticatedUser does not allow authenticated user after publishing on Linux

我正在使用 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")] 的操作:returns 401 Unauthorized。

登录用户收到 JWT 令牌,该令牌在请求中的每个下一个请求中发送回后端 header:授权:持有者 [此处令牌]。

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

此行为在 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 机器上的问题吗?

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

之前:

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

之后:

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