mvc4實現跨網域名稱cokkie共用。

來源:互聯網
上載者:User

標籤:des   blog   http   使用   io   for   ar   div   

網上搜一下都很多,按上面的代碼發現無法正常共用。我們的網站是使用IIS7

經過一段時間研究。實現了,。特將代碼共用出來,首先是c#代碼

      /// <summary>        /// 登入成功後,設定COOKIE        /// </summary>        /// <param name="entity"></param>        public void SaveUserFormsCookie(LoginUser entity, bool isRememberMe = false)        {            var userDate = SerializationHelper.JsonSerialize(entity);  //將對象序列化            var cookieTime = DateTime.Now;            var cookieExpiration = cookieTime.AddMinutes(FormsAuthentication.Timeout.TotalMinutes);            if (isRememberMe)            {                cookieExpiration = cookieTime.AddYears(1);            }            var ticket = new FormsAuthenticationTicket(1, entity.Username, cookieTime, cookieExpiration, false, userDate);            var hash = FormsAuthentication.Encrypt(ticket);            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash)            {                Expires = cookieExpiration,                Domain = FormsAuthentication.CookieDomain,                Path = FormsAuthentication.FormsCookiePath            };            HttpContext.Current.Response.Cookies.Add(cookie);        }

  然後是web配置,必須要有一個密碼編譯演算法的匹配。我們增加的cokkieY有做加密。然後如果你是.net 4 ,和4.5密碼編譯演算法還不一樣,必須統一,接下來是Web.config配置節點 <system.web>增加

  <httpRuntime targetFramework="4.0" /> 這個版本最好不同網站統一一下。按這樣的配置即可共用
<compilation debug="true" targetFramework="4.0" />    <httpRuntime targetFramework="4.0" />    <!--共用cokkies-->    <authentication mode="Forms">      <forms loginUrl="~/account/login" name="accountUser" timeout="720" domain="xxx.com" path="/"></forms>    </authentication>    <httpCookies domain="kaopu001.com" />    <machineKey validationKey="69C632A1E794CD859422A7C4F2BBD3C6783A84488AF5646B610171C058C1E2F78D8FB5823681ABD108BC1E5427C99F5EB3B69EE1D00235276EF4898F9972EA28" decryptionKey="BC5B10779346BD18168E37F5660D89A17D8B7CFF14AB0AA5" decryption="3DES" validation="SHA1"/>    

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.