Swashbuckle文档CSRF令牌.如何通过考试?文档
本文关键字:文档 考试 何通过 令牌 CSRF Swashbuckle | 更新日期: 2023-09-27 18:17:01
我使用swashbuckle来记录我的webapi 2.0 api。对于我的用例来说,它基本上是可以的,但是有些东西我无法处理。关于webapi和CSRF的资源不多,但我认为有这种保护是很重要的。所以我通过自定义头实现了它。现在我不能用我的招摇的UI了,因为这个。这将是伟大的传递一些头值在UI。这可能吗?请帮助
如果要过滤特定动词的所有操作并使用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,
});
}
}
类本身并没有很好的文档记录,但是我已经在swagger.config.cs中添加了我自己的类
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
});
};
}
}