网上搜一下都很多,按上面的代码发现无法正常共享。我们的站点是使用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"/>
mvc4实现跨域名cokkie共享。,布布扣,bubuko.com
时间: 2024-10-28 18:52:04