这里主要采用cookies共享。
步骤1:[写cookies]
//创建一个HttpCookie对象
HttpCookie cookie = new HttpCookie("uId");
//设定此cookies值
cookie.Value = "uIdValue";
cookie.Domain= "company.com";
//设定cookie的生命周期,在这里定义为一个小时
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0);
cookie.Expires = dtNow + tsMinute;
//加入此cookie
Response.Cookies.Add(cookie);
Response.Write(Request.Cookies["uId"].Value.ToString() + "Cookie创建完毕 <br > <hr > ");
步骤2:[读cookies]
HttpCookie cookie = Request.Cookies["uId"];
if (cookie != null)
{
Response.Write(Request.Cookies["uId"].Value.ToString();
}
步骤3:[删除cookies] 注销用
HttpCookie MyCo = HttpContext.Current.Request.Cookies["uId"];
if (HttpContext.Current.Request.ServerVariables["Http_Host"].IndexOf("company.com") >= 0)
{
MyCo.Domain = "company.com";
}
MyCo.Expires = DateTime.Now.AddHours(-24);
Response.Cookies.Add(MyCo);//这一句非常重要