Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.Net Core 3.1 API JWT验证在生产中失败_C#_Asp.net Core_Jwt_Asp.net Core Webapi_Asp.net Core 3.1 - Fatal编程技术网

C# ASP.Net Core 3.1 API JWT验证在生产中失败

C# ASP.Net Core 3.1 API JWT验证在生产中失败,c#,asp.net-core,jwt,asp.net-core-webapi,asp.net-core-3.1,C#,Asp.net Core,Jwt,Asp.net Core Webapi,Asp.net Core 3.1,在使用ASP.NETCore3.1API时,我在生产中遇到JWT(承载)令牌验证问题 鉴于此代码: services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwt

在使用ASP.NETCore3.1API时,我在生产中遇到JWT(承载)令牌验证问题

鉴于此代码:

services.AddAuthentication(x =>  
{  
    x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;  
    x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})  
.AddJwtBearer(x =>  
{  
    x.Authority = config["JwtOptions:Authority"];
    x.Audience = config["JwtOptions:Audience"];
}
所有工作都将在我的开发环境中进行。但是,转到生产时,发现令牌无效。Kestrel返回一个WWW-Authenticate标头,该标头显示:

Bearer error="invalid_token", error_description="The signature is invalid"
我对这种差异的原因感到困惑。让它在生产环境中工作的唯一方法似乎是改变如下设置,然而,这意味着牺牲JWT应该提供的所有安全性

x.TokenValidationParameters = new TokenValidationParameters  
{
    RequireSignedTokens = false, 
    ValidateIssuerSigningKey = false,
    ValidateIssuer = false,
    ValidateAudience = false, 
    SignatureValidator = delegate(string token, TokenValidationParameters parameters)
    {
        var jwt = new JwtSecurityToken(token);
        return jwt;
    }, 
};
欢迎提出任何意见

编辑


问题在于HTTP。使用HTTPS时,一切正常。这似乎是Microsoft.AspNetCore.Authentication.JwtBearer中的一个bug,因为我希望这也适用于HTTP。微软实际上有使用反向代理来保护红隼远离世界的手册;在这种情况下,使用HTTP应该可以。

您使用什么来签署令牌?我目前使用它来签署令牌。我希望API能够自动从发卡机构检索签名。这种情况似乎不会发生,但我不确定如何调试它以了解更多详细信息。您用于生产的域的客户端id/客户端机密的应用程序设置是否正确?另外,检查创建的承载令牌是否在Yes中有效。该令牌在jwt.io中有效。消费应用程序是桌面应用程序,因此没有秘密或域。开发和测试之间确实存在着真正的区别;只有测试API在虚拟机的Linux上运行。这可能是原因,但不知道如何找出根本原因。如果你故意使用不安全的HTTP,你就是在暗示你信任所涉及的服务器和网络。。。在这种情况下,完全不需要身份验证/授权,因此也不需要JWT。这感觉像是一个X-Y问题:为什么在HTTP上使用JWT?