asp.net-mvc - 如何在 Asp.Net MVC 生成的 cookie 中设置 sameSite=None?

标签 asp.net-mvc

我有一个 asp.net MVC 应用程序,我想为该应用程序设置所有 cookies sameSite=None。我在 web.config 中设置了以下几行,但应用程序设置了没有 SameSite=None 的 cookie。 我在 web.config 中添加了以下两个配置。请参阅下面的屏幕截图,同时放置了 .AspNet.ApplicationCookie__RequestVerificationToken cookie,但没有设置 sameSite=None。请帮忙。

<system.web>
        <httpCookies requireSSL="true"/>
        <sessionState cookieSameSite="None"/>
</system.web>

enter image description here

最佳答案

我从代码中做到了并且它有效
在 global.asax.cs 中:

 public class MvcApplication : HttpApplication
                {
                
                   protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
                   {
        .....
                        if (Request.Cookies.Count > 0)
                        {                        
                            foreach (string s in Request.Cookies.AllKeys)
                            {
                                HttpCookie c = Request.Cookies[s];
                                c.SameSite = System.Web.SameSiteMode.None;
                                Response.Cookies.Set(c);
                            }
                       }
        ....
                    }
                }

如果你想要特定的cookie

 if (Request.Cookies.Count > 0)
            {
                foreach (string s in Request.Cookies.AllKeys)
                {
                    if (s.ToLower() == "__requestverificationtoken")
                    {
                        HttpCookie c = Request.Cookies[s];
                        c.SameSite = System.Web.SameSiteMode.Strict;
                        Response.Cookies.Set(c);
                    }
                }
            }

关于asp.net-mvc - 如何在 Asp.Net MVC 生成的 cookie 中设置 sameSite=None?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63449547/

相关文章:

c# - 渲染没有 1-1 效果的图像

c# - 第三方 dll 在 ASP.NET MVC 项目中找不到其依赖项

c# - 将动态添加的列表项发布到 Controller

asp.net-mvc - OAuthAuthorizationServerProvider 在哪里检查刷新 token 过期?

c# - ASP.NET MVC : how to make all unspecified URLs direct to one controller

jquery - 部分 View 与 Json(或两者)

asp.net-mvc - MVC DB first Fix display Name

c# - Entity Framework 的悲观并发

asp.net-mvc - 如何在 asp.net mvc 中使用验证码

javascript - 将 CKEDITOR 值发布到 Asp.net Core 中的操作