C# MVC5和带基本授权标头的Swashback(.NET 4.6.1框架)
您好,我正在使用带MVC5的Swashback,我能够生成Swagger UI,但我还需要基本授权标头 我尝试了以下代码C# MVC5和带基本授权标头的Swashback(.NET 4.6.1框架),c#,.net,asp.net-mvc-5,swashbuckle,.net-4.6.1,C#,.net,Asp.net Mvc 5,Swashbuckle,.net 4.6.1,您好,我正在使用带MVC5的Swashback,我能够生成Swagger UI,但我还需要基本授权标头 我尝试了以下代码 public class AddAuthorizationHeaderParameterOperationFilter: IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
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是否支持这一点,我遇到过这样的情况,即报告了带有swagger的基本身份验证问题,但没有得到解决
我建议改为使用Rest客户端Advance Rest client或posteman等工具测试代码。感谢您的帮助。从上周开始就一直在苦苦挣扎,最后又向前迈进了一步。