虚张声势的文档 csrf 令牌。如何通过呢?文件吗?

swashbuckle document csrf token. how to pass it? document it?

我正在使用 swashbuckle 来记录我的网站api 2.0 api。基本上没问题,对于我的用例来说已经足够了,但是有些事情我无法处理。 CFRS - 好吧 - 关于 webapi 和 CSRF 的资源不多,但我认为拥有这种保护很重要。 所以我通过自定义header实现了它。现在我只是不能使用我的招摇 ui - 因为那个。在 UI 中传递一些 header 值会很棒。有可能吗?请帮忙

class 本身并没有很好的记录,但我在 swagger.config.cs

中添加了我自己的 class
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
            });
        };
    }
}

如果要过滤特定动词的所有操作并使用 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,
        });
    }
}