Cookie在域中的读写实现顶级域共享的方法

发布时间:2019-08-13编辑:脚本学堂
Cookie在域中的读写实现顶级域共享的方法

Cookie在域中的读写实现顶级域共享的方法,有需要的朋友可以参考下。
 

复制代码 代码如下:

///读写Cookie
////写入Cookies信息写入Cookies信息#region 写入Cookies信息
/**//**//**//// <summary>
/// 写入Cookies信息
/// </summary>
private void WriteCookies(string uname,string upwd)
{
    //写入Cookies中帐户信息
//    HttpCookieCollection Colcookies = Request.Cookies;   
    Colcookies = Request.Cookies;//获得Cookies集合
    HttpCookie Cookies = new HttpCookie("IRMEmail");    //创建Cookies
    Cookies.Value = uname;    //设置Cookies值
    Cookies.Domain = labDomain.Text.Trim();    //设置Cookies域
    Colcookies.Add(Cookies);    //加入Cookies集合
    Response.Cookies.Add(Cookies);

    //写入Cookies中密码信息
    Cookies = new HttpCookie("IRMPassword");
    Cookies.Value = upwd;
    Cookies.Domain = labDomain.Text.Trim();
    Colcookies.Add(Cookies);
    Response.Cookies.Add(Cookies);
}
#endregion

读取Cookies信息读取Cookies信息#region 读取Cookies信息
 

复制代码 代码如下:
/**//**//**//// <summary>
/// 读取Cookies信息
/// </summary>
private void ReadCookies()
{
    StringBuilder sbText = new StringBuilder();
    Colcookies = Request.Cookies;
    int CookiesCount = Colcookies.Count;    //Cookies中的键值数目
    for(int i=0;i<CookiesCount;i++)
    {
string strName    = Colcookies[i].Name;
string strValue    = Colcookies[i].Value;
if((strName == "IRMEmail") || (strName == "IRMPassword"))
{
    sbText.Append(strName + ":" + strValue);
    sbText.Append("<br>");
}
    }
    sbText.Append("<br>");
    LblRequest.Text=sbText.ToString();
}
#endregion