<tfoot id='iSeer'></tfoot>

    <bdo id='iSeer'></bdo><ul id='iSeer'></ul>
    <i id='iSeer'><tr id='iSeer'><dt id='iSeer'><q id='iSeer'><span id='iSeer'><b id='iSeer'><form id='iSeer'><ins id='iSeer'></ins><ul id='iSeer'></ul><sub id='iSeer'></sub></form><legend id='iSeer'></legend><bdo id='iSeer'><pre id='iSeer'><center id='iSeer'></center></pre></bdo></b><th id='iSeer'></th></span></q></dt></tr></i><div id='iSeer'><tfoot id='iSeer'></tfoot><dl id='iSeer'><fieldset id='iSeer'></fieldset></dl></div>

    <small id='iSeer'></small><noframes id='iSeer'>

      1. <legend id='iSeer'><style id='iSeer'><dir id='iSeer'><q id='iSeer'></q></dir></style></legend>

        在 ASP.NET Core 中使用防伪 cookie,但使用非默认 CookieName

        时间:2023-07-12
          <legend id='bcimj'><style id='bcimj'><dir id='bcimj'><q id='bcimj'></q></dir></style></legend>
        • <i id='bcimj'><tr id='bcimj'><dt id='bcimj'><q id='bcimj'><span id='bcimj'><b id='bcimj'><form id='bcimj'><ins id='bcimj'></ins><ul id='bcimj'></ul><sub id='bcimj'></sub></form><legend id='bcimj'></legend><bdo id='bcimj'><pre id='bcimj'><center id='bcimj'></center></pre></bdo></b><th id='bcimj'></th></span></q></dt></tr></i><div id='bcimj'><tfoot id='bcimj'></tfoot><dl id='bcimj'><fieldset id='bcimj'></fieldset></dl></div>

        • <small id='bcimj'></small><noframes id='bcimj'>

              • <bdo id='bcimj'></bdo><ul id='bcimj'></ul>
              • <tfoot id='bcimj'></tfoot>

                  <tbody id='bcimj'></tbody>

                1. 本文介绍了在 ASP.NET Core 中使用防伪 cookie,但使用非默认 CookieName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在考虑更改 ASP.NET Core 中默认防伪 cookie 的名称.

                  I'm thinking about changing name of the default antiforgery cookie in ASP.NET Core.

                  我之所以要更改 cookie 名称是为了匿名化 cookie,在我看来,最终用户没有理由能够确定该 cookie 的责任.

                  The reason why I would like to change the cookie name is to anonymize the cookie, in my opinion there is no reason why end users should be able to determine the responsibility of this cookie.

                  Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions.CookieName

                  1. 如何更改防伪 cookie 的名称?我想它应该以某种方式在 Startup.cs 文件中完成?
                  2. 更改默认防伪 cookie 的名称可能会产生哪些影响?
                  3. 如何在 ASP.NET Core 中使用防伪 cookie?
                  4. 不同的网络应用程序(使用相同的域)应该共享一个防伪 cookie,还是应该为每个网络应用程序创建单独的防伪 cookie?

                  推荐答案

                  您可以在 Startup.ConfigureServices 中设置不同的名称,如下所示:

                  You can set a different name in your Startup.ConfigureServices as in:

                  services.AddAntiforgery(opts => opts.CookieName = "MyAntiforgeryCookie");
                  

                  对于 .Net Core 2.0.0 或更高版本会有更改:

                  参考:https://docs.microsoft.com/en-us/dotnet/api/Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions?view=aspnetcore-2.0

                  为此使用以下内容:

                  services.AddAntiforgery(opts => opts.Cookie.Name = "MyAntiforgeryCookie");
                  

                  默认情况下,AddMvc() 内部调用AddAntiforgery(),这意味着您将获得默认的cookie、标题和表单名称.如果您需要/想要使用不同的名称,您可以通过如上所述手动调用 AddAntiforgery 来实现.

                  By default AddMvc() internally calls AddAntiforgery(), which means you get the default cookie, header and form names. If you need to/want to use different names, you can do so by manually calling AddAntiforgery as above.

                  如果您更改 cookie 名称,应该不会对您的应用程序产生任何影响(除非您自己添加了手动使用该 cookie 的代码).您可能还想更改标题/表单名称,例如官方 Antiforgery repo 有一个示例,它使用 Angular 并将标头更改为标准 Angular XSRF 令牌标头.

                  There should be no implications for your application if you change the cookie name (unless you added code yourself that manually used that cookie). You might also want to change the header/form name, for example the offical Antiforgery repo has an example that uses Angular and changes the header as the standard angular XSRF token header.

                  为了使用它,请将 [ValidateAntiForgeryToken] 添加到 GET 请求以外的控制器操作中.

                  In order to use it, add the [ValidateAntiForgeryToken] to controller actions other than GET requests.

                  只要您使用 asp 表单标签助手,您就不必对标准 html 表单做任何其他事情,请参阅 这个问题.

                  You have to do nothing else for standard html forms as long as you use the asp form tag helpers, see this question.

                  如果您使用 ajax 请求,那么您将需要在您的请求中包含一个标头或一个包含生成令牌的字段.你基本上需要:

                  If you use ajax requests, then you will need to include either a header or a field within your request that includes the generated token. You basically need to:

                  1. 获取IAntiforgery
                  2. 调用var tokenSet = antiforgery.GetAndStoreTokens(httpContext);
                  3. 使其可用于您的 js 代码,以便它知道值 tokenSet.RequestToken 被包含为名称为 tokenSet.FormFieldName 的字段或每个 ajax 请求中名称为 tokenSet.HeaderName 的标头.

                  1. Get an IAntiforgery
                  2. Call var tokenSet = antiforgery.GetAndStoreTokens(httpContext);
                  3. Make it available to your js code so it knows about the value tokenSet.RequestToken to be included as either a field with name tokenSet.FormFieldName or a header with name tokenSet.HeaderName within each ajax request.

                  • 一些选项,例如将令牌渲染到 js 布局中脚本部分内的 JS 对象,添加 JS 可读 cookie,如 angular 示例中,继续渲染包含在 ajax 请求中的隐藏字段
                  • 有一个很好的选项概述 在这个答案中

                  目标是让 POST/PUT/DELETE/PATCH 请求包含 2 个内容:

                  The aim is for POST/PUT/DELETE/PATCH requests to include 2 things:

                  • 防伪 cookie
                  • 带有令牌的字段/标题

                  所以防伪中间件可以验证没有 XSRF.

                  So the antiforgery middleware can validate there was no XSRF.

                  关于 cookie 名称/域的更新

                  合理的默认设置是每个应用程序都有自己的 cookie.您通常使用默认方法得到它,因为 cookie 上没有专门设置域,因此 cookie 从请求中获取域.这意味着不同的应用程序使用不同的 cookie,除非应用程序托管在同一个域中.

                  The sensible default is for each application to have its own cookie. You mostly get that with the default approach as no domain is specifically set on the cookie, so the cookie takes the domain from the request. That would mean different cookies for different applications unless the appliacations are hosted with the same domain.

                  • 详细了解 Cookie 的工作原理此处李>

                  您可能只想在特殊情况下共享 cookie,例如,如果您有 2 个应用程序,其中应用程序 A 中的一个表单发布到应用程序 B.在这些情况下,请确保您使用的域/子域与这两个应用程序以及两者都匹配使用相同的 cookie 名称.

                  You might only want to share the cookie in special cases, for example if you have 2 applications where a form in app A posts to app B. In those cases make sure you use a domain/subdomain that matches both applications and both use the same cookiea name.

                  • 了解更多关于 XSRF 这里

                  这篇关于在 ASP.NET Core 中使用防伪 cookie,但使用非默认 CookieName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:无法将索引 35 处的字节 [FC] 从指定代码页转换为 Unicode 下一篇:向接受 ICollection&lt;IFormFile&gt; 的 ASP.NET 控制器提交多个文件

                  相关文章

                  <i id='o40SK'><tr id='o40SK'><dt id='o40SK'><q id='o40SK'><span id='o40SK'><b id='o40SK'><form id='o40SK'><ins id='o40SK'></ins><ul id='o40SK'></ul><sub id='o40SK'></sub></form><legend id='o40SK'></legend><bdo id='o40SK'><pre id='o40SK'><center id='o40SK'></center></pre></bdo></b><th id='o40SK'></th></span></q></dt></tr></i><div id='o40SK'><tfoot id='o40SK'></tfoot><dl id='o40SK'><fieldset id='o40SK'></fieldset></dl></div>

                    • <bdo id='o40SK'></bdo><ul id='o40SK'></ul>

                      <tfoot id='o40SK'></tfoot>
                      <legend id='o40SK'><style id='o40SK'><dir id='o40SK'><q id='o40SK'></q></dir></style></legend>
                    1. <small id='o40SK'></small><noframes id='o40SK'>