C#学习教程:使用授权标头(Bearer)设置Swagger(ASP.NETCore)我正在尝试大摇大摆地打电话。这些调用必须包含Authorization标头,我正在使用Bearer身份验证。从Postman等第三方应用程序调用是没有问题的。但是我在设置swagger标头时遇到问题(出于某种原因我没有收到标头)。这就是现在的类型:"host":"localhost:50352","basePath":"/","schemes":["http","https"],"securityDefinitions":{"Bearer":{"name":"Authorization","in":"header","type":"apiKey","description":"HTTP/HTTPSBearer"}},"paths":{"/v1/{subAccountId}/test1":{“post”:{“tags”:[“auth”],“operationId”:“op1”,“consumes”:[“application/json”,“application/html”],“produces”:[“application/json","application/html"],"parameters":[{"name":"subAccountId","in":"path","required":true,"type":"string"}],"security":[{"Bearer":[]}],"responses":{"204":{"description":"NoContent"},"400":{"description":"BadRequest","schema":{"$ref":"#/definitions/ErrorResponse"}},"401":{"description":"Unauthorized","schema":{"$ref":"#/definitions/ErrorResponse"}},"500":{"description":"InternalServerError","schema":{"$ref":"#/definitions/ErrorResponse"}}},"deprecated":false}},首先,您可以使用Swashbuckle.AspNetCorenuget包自动生成你的swagger定义(在2.3.0上测试过)安装包后,在Startup.cs中的方法ConfigureServicesservices.AddSwaggerGen(c=>{c.SwaggerDoc("v1",newInfo{Title="Youapititle",Version="v1"});c.AddSecurityDefinition("Bearer",newApiKeyScheme{In="header",Description="PleaseenterJWTwithBearerintofield",Name="Authorization",Type="apiKey"});c.AddSecurityRequirement(newDictionary>{{"Bearer",Enumerable.Empty()},});});然后您可以使用页面右上角的“授权”按钮。至少你可以尝试使用这个包来生成有效的swagger定义目前,Swagger具有使用JWT-token进行身份验证的功能,并且可以自动将令牌添加到标头中,我已经在这里回答了。以上就是C#学习教程:使用授权头(Bearer)设置Swagger(ASP.NETCore)共享的所有内容。如果对你有用,需要了解更多C#学习教程,希望大家多加关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击右转联系管理员删除。如需转载请注明出处:
