然后以下是我的想法
不用Session做登录凭证而用Cookies来做登录凭证
1:然后在IIS中建立两个网站News.MySite.com,Blog.MySite.com (注这些在要Hosts文件中进行转向,不懂可以网上搜),注意一定要有域名的网站不然的话如网站主机头为127.0.0.1或者localhost这样的主机头没有办法保存域Cookies
2:在两个网站的Web.config中添加appsetting
<appSettings>
<add key="RootDomain" value="mysite.com"/>
<add key="PrivateKey" value="12345678"/>
</appSettings>
这是为了方便网站以后换域名的时候不用更改代码,PrivateKey是防止篡改Cookies而效仿网银功能添加多一个MD5验证功能
3:编写Cookies操作类
view sourceprint?
004
|
namespace Z.Core.Tools
|
014
|
/// <param name="name">名称</param>
|
015
|
/// <param name="value">值</param>
|
016
|
public static void Set(string name, string value)
|
024
|
/// <param name="name">名称</param>
|
025
|
/// <param name="value">值</param>
|
026
|
/// <param name="expiresDays">过期时间</param>
|
027
|
public static void Set(string name, string value, int expiresDays)
|
030
|
foreach (string item in HttpContext.Current.Response.Cookies.AllKeys)
|
032
|
//判断为和当前已有的Cookie相同的时候进行remove
|
035
|
HttpContext.Current.Response.Cookies.Remove(name);
|
038
|
HttpCookie MyCookie = new HttpCookie(name);
|
039
|
if (System.Configuration.ConfigurationManager.AppSettings["RootDomain"] == null)
|
041
|
throw new Exception(Lang.Define.Get(Lang.DefineEnum.RootDomain_未设置));
|
043
|
MyCookie.Domain = System.Configuration.ConfigurationManager.AppSettings["RootDomain"];
|
046
|
MyCookie.Value = System.Web.HttpUtility.UrlEncode(value).Replace("+", "%20");
|
048
|
//如果值为null的话说明删除这个cookie
|
049
|
if (value == null && expiresDays == 0)
|
053
|
if (expiresDays != 0)
|
055
|
DateTime expires = DateTime.Now.AddDays(expiresDays);
|
056
|
MyCookie.Expires = expires;
|
058
|
HttpContext.Current.Response.Cookies.Add(MyCookie);
|
064
|
/// <param name="name">名称</param>
|
065
|
public static void Delele(string name)
|
073
|
/// <param name="name">名称</param>
|
074
|
/// <returns>值</returns>
|
075
|
public static string Get(string name)
|
077
|
string result = null;
|
078
|
foreach (string item in HttpContext.Current.Response.Cookies.AllKeys)
|
082
|
if (HttpContext.Current.Response.Cookies[name].Expires > DateTime.Now || HttpContext.Current.Response.Cookies[name].Expires == new DateTime(1, 1, 1))
|
084
|
//如果判断到这个Cookie是有效的,取这个有效的新值
|
085
|
result = System.Web.HttpUtility.UrlDecode(HttpContext.Current.Response.Cookies[name].Value);
|
096
|
if (HttpContext.Current.Request.Cookies[name] != null)
|
098
|
result = System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[name].Value.Replace("%20", "+"));
|
106
|
public static void Clear()
|
108
|
for (int i = 0; i <= HttpContext.Current.Request.Cookies.Count - 1; i++)
|
110
|
//当Cookies的名称不为ASP.NET_SessionID的时候将他删除,因为删除了这个Cookies的话会导致重创建Session链接
|
111
|
if (HttpContext.Current.Request.Cookies[i].Name.ToLower() != "asp.net_sessionid")
|
113
|
Set(HttpContext.Current.Request.Cookies[i].Name, "", -1);
|
4:编写登录凭证类
view sourceprint?
002
|
using System.Collections.Generic;
|
006
|
namespace Z.Core.Tools
|
011
|
public class CookieGroupTemplate
|
016
|
public static string UserCode
|
021
|
return Z.Core.Tools.Cookie.Get("UserCode");
|
025
|
Z.Core.Tools.Cookie.Set("UserCode", value);
|
033
|
public static string UserName
|
038
|
return Z.Core.Tools.Cookie.Get("UserName");
|
042
|
Z.Core.Tools.Cookie.Set("UserName", value);
|
050
|
public static string ParentCode
|
055
|
return Z.Core.Tools.Cookie.Get("ParentCode"); ;
|
059
|
Z.Core.Tools.Cookie.Set("ParentCode", value);
|
067
|
public static string ParentName
|
072
|
return Z.Core.Tools.Cookie.Get("ParentName");
|
076
|
Z.Core.Tools.Cookie.Set("ParentName", value);
|
084
|
public static string Groups
|
089
|
return Z.Core.Tools.Cookie.Get("Groups"); ;
|
093
|
Z.Core.Tools.Cookie.Set("Groups", value);
|
101
|
public static string OperateFrom
|
105
|
return Z.Core.Tools.Cookie.Get("OperateFrom");
|
109
|
Z.Core.Tools.Cookie.Set("OperateFrom", value);
|
116
|
static List<string> CookieKeys = new List<string>()
|
118
|
"UserCode","UserName","ParentCode","ParentName","Groups","OperateFrom"
|
127
|
foreach (var s in CookieKeys)
|
131
|
key += Cookie.Get(s);
|
134
|
key += SettingGroupTemplate.PrivateKey;
|
136
|
Cookie.Set("PrivateKey", key);
|
142
|
static void CheckKey()
|
145
|
foreach (var s in CookieKeys)
|
149
|
key += Cookie.Get(s);
|
152
|
string privateKey = Cookie.Get("PrivateKey");
|
153
|
if (privateKey == null)
|
156
|
foreach (var s in CookieKeys)
|
168
|
key += SettingGroupTemplate.PrivateKey;
|
170
|
if (privateKey == null)
|
174
|
if (key != privateKey)
|
176
|
throw new ExceptionMessage(Lang.DefineEnum.Cookie验证出错.Define());
|
----------------------------------------
好了,我默认在我的Cookies类中添加了几个常用到的值为读取这些Cookies的时候进行MD5验证,以保证Cookies的安全性
然后只要在你的网站项目中引用上面两个类,
然后在任意一个网站写入代码
Z.Core.Tools.CookieGroupTemplate.UserCode = "123";
然后在其他网站中用代码
Z.Core.Tools.CookieGroupTemplate.UserCode;
都可以读取得到这个登录用户的ID
是不是很简单啊。。。。
出处:http://www.cnblogs.com/JerryBaxia/archive/2010/08/22/1805648.html
欢迎转载,但需保留版权。