解决.NET核心API的JWT承载身份验证问题

解决.NET核心API的JWT承载身份验证问题,jwt,postman,asp.net-core-3.1,bearer-token,Jwt,Postman,Asp.net Core 3.1,Bearer Token,我使用.NETCore3.1API,我想配置一个JWTBear令牌。 我已经配置了一个方法,生成一个ValidateLifetime为一天的令牌 然后,我将[authorize]放在我的usercontroller方法上,并使用我的令牌和选择“Bearer token as authorization method”对所有这些进行了测试,但我不知道为什么它不起作用 我填写了之前由GenerateToken方法生成的令牌,我试图只输入jwt令牌的头部分,jwt令牌的头/内容/签名,但它不起作用 有

我使用.NETCore3.1API,我想配置一个JWTBear令牌。 我已经配置了一个方法,生成一个ValidateLifetime为一天的令牌

然后,我将[authorize]放在我的usercontroller方法上,并使用我的令牌和选择“Bearer token as authorization method”对所有这些进行了测试,但我不知道为什么它不起作用

我填写了之前由GenerateToken方法生成的令牌,我试图只输入jwt令牌的头部分,jwt令牌的头/内容/签名,但它不起作用

有人会有视频或教程解释如何测试“熊令牌”作为身份验证模式吗

//生成的令牌的示例: Eyjhbgcioijiuzi1NiiiInr5cIkPxVcJ9.Eyjodhrwoi8Vc2NozW1Hcy54BwxZb2Fwlm9Yzy93cyMd8Ymda1lZw50AxR5L2Nc2Nc2Nc2Nc2Ncy2Nc9Uy1l2Nc1l2Nc9UyWl2Nc1l2Nc1l2Nc1l2Nc1l2Nc1l2Ncy9Uy1l2Ny1l2Ny1l2Ny1l2Ny1l2Ny1l2Ny1l2Ny1l2Ny2Ny1l2Ny2Ny1l2Ny2Ny1l2Ny1l2Ny1l2Ny2Ny2Ny1l2Ny1Iy1Iy1Iy1IY2xHAW1ZL3JVbGUIJBZG1PBIJ9.E9TnS62nv10gNH8U03OPhK_qrgletns7YJHBH4I0E

        {
            var claims = new List<Claim>{
            new Claim(ClaimTypes.Name , UserNAME),
            new Claim(ClaimTypes.NameIdentifier, userId),
            new Claim(JwtRegisteredClaimNames.Nbf,new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString()),
            new Claim(JwtRegisteredClaimNames.Exp,new DateTimeOffset(DateTime.Now.AddDays(1)).ToUnixTimeSeconds().ToString())
            };
            claims.Add(new Claim(ClaimTypes.Role, "Admin"));
            var token = new JwtSecurityToken(
                    new JwtHeader(
                        new SigningCredentials(
                            new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SECRET_KEY)),
                            SecurityAlgorithms.HmacSha256
                            )),
                        new JwtPayload(claims));
            var output = new
            {
                Accces_Token = new JwtSecurityTokenHandler().WriteToken(token),
                UserName = UserNAME
            };
            return output;




        }
//this is my authentication services
 services.AddAuthentication(options => {
                options.DefaultAuthenticateScheme = "jwtBearer";
                options.DefaultChallengeScheme = "jwtBearer";
            }).AddJwtBearer("jwtBearer", jwtoptions => {
                jwtoptions.TokenValidationParameters = new TokenValidationParameters()
                {
                    IssuerSigningKey = SIGNING_KEY,
                    ValidateIssuer = false,
                    ValidateAudience = false,
                    ValidateLifetime = true,
                    ClockSkew = TimeSpan.FromMinutes(5) };
            });
//this is my IApplicationBuilder application configure : {
  app.UseAuthentication();
            app.UseAuthorization();```}
{
var索赔=新列表{
新索赔(ClaimTypes.Name、用户名),
新索赔(ClaimTypes.NameIdentifier,userId),
新声明(JwtRegisteredClaimNames.Nbf,new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds().ToString()),
新声明(JwtRegisteredClaimNames.Exp,新的DateTimeOffset(DateTime.Now.AddDays(1)).ToUnixTimeSeconds().ToString())
};
添加(新索赔(ClaimTypes.Role,“Admin”);
var token=新的JwtSecurityToken(
新JwtHeader(
新的签约证书(
新的SymmetricSecurityKey(Encoding.UTF8.GetBytes(SECRET_KEY)),
SecurityAlgorithms.HmacSha256
)),
新的JWT有效载荷(索赔);
var输出=新
{
Accces_Token=new JwtSecurityTokenHandler().WriteToken(令牌),
用户名=用户名
};
返回输出;
}
//这是我的身份验证服务
services.AddAuthentication(选项=>{
options.DefaultAuthenticateScheme=“jwtBearer”;
options.DefaultChallengeScheme=“jwtBearer”;
}).AddJwtBearer(“jwtBearer”,jwtoptions=>{
jwtoptions.TokenValidationParameters=新的TokenValidationParameters()
{
IssuerSigningKey=签名密钥,
validateisuer=false,
ValidateAudience=false,
ValidateLifetime=true,
ClockSkew=时间跨度从分钟(5)};
});
//这是我的IApplicationBuilder应用程序配置:{
app.UseAuthentication();
app.UseAuthorization();``}

从生成令牌的代码来看,
签名密钥
是一个字符串,而
issueSigningKey
是一种
SecurityKey
类型。因此,在配置中,需要对其进行更改

jwtoptions.TokenValidationParameters = new TokenValidationParameters()
            {
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(SIGNING_KEY)),
               
            };
您测试令牌的方式可能不正确。生成令牌后,将其放入请求头中记下持票人


这是我的签名密钥,正如您所看到的,它不是一个字符串
公共静态只读SymmetricSecurityKey签名密钥=新的SymmetricSecurityKey(Encoding.UTF8.GetBytes(SECRET\u KEY))@MUTIJIMA,我测试你的代码,它可以正常工作,你可能会丢失你可以参考答案的承载者。