繁体   English   中英

ASP.NET Core 2.0 中的默认防伪禁用响应缓存

[英]Default Anti forgery disabling Response Caching in ASP.NET Core 2.0

我正在尝试对某些操作实施响应缓存并禁用默认的防伪造行为,但在每次请求时我都会收到“

警告:Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery[8] 'Cache-Control' 和 'Pragma' 标头已被覆盖并分别设置为 'no-cache, no-store' 和 'no-cache' 以防止缓存这个回应。 不应缓存任何使用防伪的响应。

我在操作上定义了IgnoreAntiforgeryToken 还有其他方法可以关闭默认的防伪造功能吗?

启动.cs

public void ConfigureServices(IServiceCollection services)
{
    .
    .
    .
    services.AddResponseCaching();

    services.AddMvc(options =>
            {
                options.CacheProfiles.Add("Default",
                    new CacheProfile()
                    {
                        Duration = 240
                    });
                options.CacheProfiles.Add("NoCache",
                    new CacheProfile()
                    {
                        Location = ResponseCacheLocation.None,
                        NoStore = true
                    });
            })
            .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    .
    .
    .
    app.UseResponseCaching();
}

Controller.cs

[IgnoreAntiforgeryToken]
[ResponseCache(CacheProfileName = "Default", VaryByQueryKeys = new string[] { "id" })]
[HttpGet("/Shop/Category/{id:int}", Name = "Category")]
public IActionResult Category(int id)
{
    var result = this.AppHelper.ModelHelper.GetCategoryWrap(id);

    return View(result);
}

在视图中生成表单时将antiforgery参数设置为 false

using (Html.BeginForm(
    "MyAction",
    "MyController",
    null,
    FormMethod.Post,
    false, //<--this
    new { if = "myForm", @class="mycssclass" }))

在表单代码中使用asp-antiforgery标签助手

<form action="" method="post" asp-antiforgery="false">
    <div class="form-group">
        <label for="remarks">enter remarks</label>
        <textarea class="form-control" id="remarks" rows="3" name="remarks">
            @if (videoViewModel.Remarks != null) {
                @videoViewModel.Remarks
            }
        </textarea>
    </div>
    <button type="submit" class="btn btn-secondary mb-2">Submit</button>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM