本文主要是介绍aspnetcore使用jwt时一直提示401 authorization,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
测试aspnetcore使用Jwt做认证授权的时候,一直提示401 Authorization
最后发现问题所在,希望能有所帮助
1.检查注册了认证和授权中间件 缺一不可
/*认证*/app.UseAuthentication();/*授权*/app.UseAuthorization();
2.检查swagger的配置项
builder.Services.AddSwaggerGen(option => {option.SwaggerDoc("v1", new OpenApiInfo{Version = "v1",Title = "",Description = $"API描述"});var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";//IncludeXmlComments 第二参数 true 则显示 控制器 注释option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);var scheme = new OpenApiSecurityScheme(){Description = "Authorization header. \r\nExample: 'Bearer 12345abcdef'",Reference = new OpenApiReference{Type = ReferenceType.SecurityScheme,Id = "Authorization"},Scheme = "oauth2",Name = "Authorization",In = ParameterLocation.Header,Type = SecuritySchemeType.ApiKey,};option.AddSecurityDefinition("Authorization", scheme);var requirement = new OpenApiSecurityRequirement();requirement[scheme] = new List<string>();option.AddSecurityRequirement(requirement);
});
这篇关于aspnetcore使用jwt时一直提示401 authorization的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!