• asp.net 二级域名表单认证情况下共享Cookie


       二级域名之间共享Cookie,很重要的一点就是配置,如下:

    domain设置为.ahdqxx.com,如果你的域名是www.ahdqxx.com,mall.ahdqxx.com,那么请设置你的domain为.ahdqxx.com

    path设置为/

    <authentication mode="Forms">
          <forms name="DQ.AUTH" loginUrl="http://www.ahdqxx.com/Login/Index" protection="All" domain=".ahdqxx.com" timeout="43200" path="/" requireSSL="false" slidingExpiration="true" />
        </authentication>

    第二重点的就是登陆时候Cookie设置,

    不要忘记使用之前配置的东西来设置 Cookie(FormsAuthentication.FormsCookiePath,FormsAuthentication.CookieDomain)

          public virtual void SignIn(Customer customer, bool createPersistentCookie)
            {
                var now = DateTime.UtcNow.ToLocalTime();
    
                var userdata = JsonConvert.SerializeObject(new SimpleUser { Name = _customerSettings.UsernamesEnabled ? customer.Username : customer.Email, ID = customer.CustomerGuid });
    
                var ticket = new FormsAuthenticationTicket(
                    1 /*version*/,
                    _customerSettings.UsernamesEnabled ? customer.Username : customer.Email,
                    now,
                    now.Add(_expirationTimeSpan),
                    createPersistentCookie,
                    userdata,
                    FormsAuthentication.FormsCookiePath);
    
    
    
                var encryptedTicket = FormsAuthentication.Encrypt(ticket);
    
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                cookie.HttpOnly = true;
                if (ticket.IsPersistent)
                {
                    cookie.Expires = ticket.Expiration;
                }
                cookie.Secure = FormsAuthentication.RequireSSL;
                cookie.Path = FormsAuthentication.FormsCookiePath;
                if (FormsAuthentication.CookieDomain != null)
                {
                    cookie.Domain = FormsAuthentication.CookieDomain;
                }
    
                _httpContext.Response.Cookies.Add(cookie);
                _cachedCustomer = customer;
            }
    

    容易犯得的错误,如果你在配置中使用了machineKey节点,请保证相关站点使用相同的machineKey

  • 相关阅读:
    Drupal Coder 模块远程命令执行分析(SA-CONTRIB-2016-039)
    Python 实现 ZoomEye API SDK
    程序员互动联盟第一届编码大赛第二题解题分享
    python中各进制之间的转换
    记一次ctf比赛解密题的解决(可逆加密基本破解之暴力破解)
    使用JsonConfig控制JSON lib序列化
    openMRS项目
    Harmonic Number(调和级数+欧拉常数)
    Pairs Forming LCM(素因子分解)
    Uva 11395 Sigma Function (因子和)
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/7286116.html
Copyright © 2020-2023  润新知