在使用 asp.net core 3.0 时传递有效令牌时,邮递员返回 401 [英] Postman returning with 401 unothorized when valid token is passed while working with asp.net core 3.0

查看:26
本文介绍了在使用 asp.net core 3.0 时传递有效令牌时,邮递员返回 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在启动时有以下代码:

I have following code in startup:

     services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                 .AddJwtBearer(options => {
                     options.TokenValidationParameters = new TokenValidationParameters
                     {
                         ValidateIssuerSigningKey = true,
                         IssuerSigningKey = new SymmetricSecurityKey(key),
                         ValidateIssuer = false,
                         ValidateAudience = false,
                     };
                 });

在身份验证控制器的身份验证类中,我有以下代码:

In Authentication class of auth controller I have following code:

 var tokenHandler = new JwtSecurityTokenHandler();
            var key = Encoding.ASCII.GetBytes(_config.GetSection("AppSettings:Secret").Value);
                     var tokenDescriptor = new SecurityTokenDescripto
         {
            Subject = new ClaimsIdentity(new Claim[]
            {
            new Claim (ClaimTypes.NameIdentifier, user.Id.ToString ()),
            new Claim (ClaimTypes.Name, user.Username)
            }),
            Expires = DateTime.Now.AddDays(1),
            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), 
          SecurityAlgorithms.HmacSha512Signature)
        };
        var token = tokenHandler.CreateToken(tokenDescriptor);
        var tokenString = tokenHandler.WriteToken(token);

这将返回 tokenString.从邮递员传递带有令牌字符串字符串的 url 时返回 401 未经授权.网址是:

This returns tokenString. while passing url with token string string from postman returns 401 unauthorized. The url is:

<代码>本地主机:5000/API/酒店/验证=承载eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIxIiwidW5pcXVlX25hbWUiOiJSdXBhayIsIm5iZiI6MTU3NTcxNDU2MCwiZXhwIjoxNTc1ODAwOTYwLCJpYXQiOjE1NzU3MTQ1NjB9.68L1K3cwRDz7CkL2MP6LdESYO0-2rG5wkyURLzvVIrkg_5XcPb1qVoP2pQgEB8DxbTNVCaBwLV_OsIg2GtTJXg

推荐答案

您能否将令牌添加到邮递员的请求标头中,而不是从 queryString 中添加.key: 认证值: Bearer {your jwt}

Can you just add your token to your request's header from postman, not from queryString. key: Authentication value: Bearer {your jwt}

这篇关于在使用 asp.net core 3.0 时传递有效令牌时,邮递员返回 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆