我有一个 ASP .NET Core web api,我生成了一个 JWT 令牌用于授权目的,但是每当我发出请求时,我都会收到401 - Unauthorized。
操作顺序:
1. GET for token
2. GET for user <-- 401
Run Code Online (Sandbox Code Playgroud)
我在 jwt.io 上检查了我的令牌,它是正确的。 当我删除[Authorize] attrivute 时一切正常
启动文件
public void ConfigureServices(IServiceCollection services)
{
IdentityModelEventSource.ShowPII = true;
var appSettingsSection = Configuration.GetSection("Jwt");
services.Configure<JwtSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<JwtSettings>();
services.AddControllers();
services.AddOptions();
services.AddAuthentication(x =>
{
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x=>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuerSigningKey …Run Code Online (Sandbox Code Playgroud)