我正在使用 swashbuckle 来记录我的 webapi 2.0 api。基本上没问题,对于我的用例来说已经足够了,但是有些事情我无法处理。CFRS - 好吧 - 关于 webapi 和 CSRF 的资源不多,但我认为拥有这种保护很重要。所以我通过自定义标头实现了它。现在我只是不能使用我的招摇用户界面——正因为如此。在 UI 中传递一些标头值会很棒。有可能吗?请帮忙
问问题
671 次
2 回答
1
如果要过滤特定动词的所有操作并使用 OperationFilterContext
c.OperationFilter<CsrfOperationFilter>();
public class CsrfOperationFilter : IOperationFilter
{
/// <summary>
/// Applies the specified operation.
/// </summary>
/// <param name="operation">The operation.</param>
/// <param name="context">The context.</param>
public void Apply(Operation operation, OperationFilterContext context)
{
if (string.Equals(context.ApiDescription.HttpMethod, "Get", System.StringComparison.OrdinalIgnoreCase))
{
return;
}
if (operation.Parameters == null)
{
operation.Parameters = new List<IParameter>();
}
operation.Parameters.Add(new NonBodyParameter
{
Name = "csrf_token",
In = "header",
Type = "string",
Required = true,
});
}
}
于 2019-01-10T00:35:36.050 回答
0
该类本身并没有很好的记录,但是我在 swagger.config.cs 中添加了我的 ownn 类
c.OperationFilter<CsrfDocumentFiler>();
并像这样实现它:
public class CsrfDocumentFiler: IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
if (operation.operationId != "Token_GetToken")
{
if (operation.parameters == null)
operation.parameters = new List<Parameter>();
operation.parameters.Add(new Parameter
{
name = "__RequestVerificationToken",
@in = "header",
type = "string",
required = true
});
};
}
}
于 2015-10-11T05:03:59.277 回答