• C#处理浏览器SameSite问题


    WebHelper.cs修改如下两个方法,加入cookie.SameSite = SameSiteMode.Lax; cookie.Secure = false;两句代码

            public static void WriteCookie(string strName, string strValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Value = strValue;
                cookie.SameSite = SameSiteMode.Lax;
                cookie.Secure = false;
                HttpContext.Current.Response.AppendCookie(cookie);
            }
            public static void WriteCookie(string strName, string strValue, int expires)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie.Value = strValue;
                cookie.SameSite = SameSiteMode.Lax;
                cookie.Secure = false;
                cookie.Expires = DateTime.Now.AddMinutes(expires);
                HttpContext.Current.Response.AppendCookie(cookie);
            }
    

      

    Web.config文件加入如下配置:

    <system.web>
        <anonymousIdentification cookieRequireSSL="false" />
        <!-- No config attribute for SameSite -->
        <authentication>
            <forms cookieSameSite="Lax" requireSSL="false" />
        </authentication>
        <!-- No config attribute for SameSite -->
        <roleManager cookieRequireSSL="false" />
        <!-- No config attribute for Secure -->
        <sessionState mode="InProc" timeout="180" cookieSameSite="Lax"/>
        ...
      </system.web>
    

    js退出登录逻辑中加入清理cookie的方法

            var loginout = function () { // 安全退出
                            ...
                            clearCookieAll();
                            ...
            }
            // 清理全部cookie
            var clearCookieAll = function() {
                var keys = document.cookie.match(/[^ =;]+(?==)/g)
                if (keys) {
                    for (var i = keys.length; i--;) {
                        document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如:m.ratingdog.cn
                        document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString() // 清除当前域名下的,例如 .m.ratingdog.cn
                        document.cookie = keys[i] + '=0;path=/;domain=ratingdog.cn;expires=' + new Date(0).toUTCString() // 清除一级域名下的或指定的,例如 .ratingdog.cn
                    }
                }
            }
    

      

    参考链接:
    https://www.cnblogs.com/wxx/p/12590007.html
    https://docs.microsoft.com/en-us/aspnet/samesite/system-web-samesite

  • 相关阅读:
    转数组
    字符串分割(分行)
    字符串操作:判断相等、判断首尾、大小写转换
    字符串操作:索引位置、去空格、替换字符串
    数组(遍历、转置、元素替换、排序、复制)
    专利申请教程
    循环语句
    条件语句
    输入
    h.264直接预测
  • 原文地址:https://www.cnblogs.com/guxingzhe/p/14153625.html
Copyright © 2020-2023  润新知