首页 > 解决方案 > Swagger Bearer Authentication 未显示 [GET] 请求的授权 JWT 令牌字段

问题描述

授权字段显示HTTP POST请求,但未显示GET请求添加令牌以进行 Web API 身份验证。

在此处输入图像描述

在此处输入图像描述

config.EnableSwagger(c =>
            {
                c.SingleApiVersion("v1", "Mach.CharterPad.API");
                c.OperationFilter<SwaggerAuthorizationFilter>();
                c.RootUrl(req => $"{req.RequestUri.GetLeftPart(UriPartial.Authority)}{req.GetConfiguration().VirtualPathRoot.TrimEnd('/')}{appvirtualpath}/api");
            }).EnableSwaggerUi();
public class SwaggerAuthorizationFilter : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            if (operation.parameters != null)
            {
                operation.parameters.Add(new Parameter
                {
                    name = "Authorization",
                    @in = "header",
                    description = "access token",
                    required = false,
                    type = "string"
                });
            }
        }
    }

标签: asp.net-web-apiswaggerswagger-uiswashbuckle

解决方案


我已经更新了 API 参数并且工作正常。

[Route("")]
public IHttpActionResult Get([FromUri] Paging paging)
{
    var result = TripManager.Get(paging.Index, paging.Size);
    return Ok(result != null ? new ApiResponse(true, "Results found", result) : new ApiResponse(false, "No record found", result));
}

在此处输入图像描述


推荐阅读