具有基本授权的 MVC5 和 Swashbuckle Header (.NET 4.6.1 Framework)
MVC5 and Swashbuckle with Basic Authorization Header (.NET 4.6.1 Framework)
您好,我在 MVC5 中使用 Swashbuckle,我能够生成 Swagger UI,但我还需要基本授权 Header。
我试过下面的代码
public class AddAuthorizationHeaderParameterOperationFilter: IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline();
var isAuthorized = filterPipeline
.Select(filterInfo => filterInfo.Instance)
.Any(filter => filter is IAuthorizationFilter);
var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();
if (isAuthorized && !allowAnonymous)
{
operation.parameters.Add(new Parameter {
name = "Authorization",
@in = "header",
description = "access token",
required = true,
type = "string"
});
}
}
}
并在 web.config
中添加了以下内容
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
获取无法加载错误 System.Web.Http 4.0.0
我怀疑 swagger 是否支持这个,我遇到了这个https://github.com/swagger-api/swagger-ui/issues/1566,其中报告了带有 swagger 问题的基本身份验证,但没有得到解决。
我建议改为使用 Rest-client(Advance Rest 客户端或 posteman 等)工具测试您的代码。
您好,我在 MVC5 中使用 Swashbuckle,我能够生成 Swagger UI,但我还需要基本授权 Header。
我试过下面的代码
public class AddAuthorizationHeaderParameterOperationFilter: IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline();
var isAuthorized = filterPipeline
.Select(filterInfo => filterInfo.Instance)
.Any(filter => filter is IAuthorizationFilter);
var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();
if (isAuthorized && !allowAnonymous)
{
operation.parameters.Add(new Parameter {
name = "Authorization",
@in = "header",
description = "access token",
required = true,
type = "string"
});
}
}
}
并在 web.config
中添加了以下内容<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
获取无法加载错误 System.Web.Http 4.0.0
我怀疑 swagger 是否支持这个,我遇到了这个https://github.com/swagger-api/swagger-ui/issues/1566,其中报告了带有 swagger 问题的基本身份验证,但没有得到解决。
我建议改为使用 Rest-client(Advance Rest 客户端或 posteman 等)工具测试您的代码。