本文介绍了使用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,
};
});
在auth控制器的身份验证类中,我有以下代码:
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。
的URL是:
This returns tokenString.while passing url with token string string from postman returns 401 unauthorized.The url is:
localhost:5000/api/hotel/?Authentication=Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIxIiwidW5pcXVlX25hbWUiOiJSdXBhayIsIm5iZiI6MTU3NTcxNDU2MCwiZXhwIjoxNTc1ODAwOTYwLCJpYXQiOjE1NzU3MTQ1NjB9.68L1K3cwRDz7CkL2MP6LdESYO0-2rG5wkyURLzvVIrkg_5XcPb1qVoP2pQgEB8DxbTNVCaBwLV_OsIg2GtTJXg
推荐答案
可以将标头添加到您的标头中吗来自邮递员,而不是来自queryString。
密钥:验证值: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返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!