有个项目药公用数据库,共享登陆,及在一个站点登陆后,所有的二级域名都共享登陆,不需要登陆了。下面简单的记录下。
我用的共享Cookie。
首先的首先在配置文件加入cookie设置:
在 <system.web>节点下:
<authentication mode="Forms">
<forms name=".DottextCookie" loginUrl="login.aspx" protection="All" timeout="480" path="/" domain=".wangzihao.com"/>
</authentication>
我们在登陆的时候可以正常存储cookie,只需要加上一句如下:
//存cookie string adminname = Utils.GetCookie("AdminName", "HGcms"); string adminpwd = Utils.GetCookie("AdminPwd", "HGcms"); /// <summary> /// 写cookie值 /// </summary> /// <param name="strName">名称</param> /// <param name="strValue">值</param> public static void WriteCookie(string strName, string key, string strValue) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie[key] = UrlEncode(strValue); cookie.Domain = FormsAuthentication.CookieDomain; HttpContext.Current.Response.AppendCookie(cookie); }
上面的在登录页面已经完成了。下面就是在二级域名下的读cookiek了,你可以像在登陆页面一样来读取cookie.
//读cookie string adminname = Utils.GetCookie("AdminName", "HGcms"); string adminpwd = Utils.GetCookie("AdminPwd", "HGcms"); /// <summary> /// 读cookie值 /// </summary> /// <param name="strName">名称</param> /// <returns>cookie值</returns> public static string GetCookie(string strName, string key) { if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null) return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString()); return ""; }