#region关于操作Cookie的方法
///<summary>
///创建cookie值
///</summary>
///<param name="cookieName">cookie名称</param>
///<param name="cookieValue">cookie值</param>
///<param name="cookieTime">cookie有效时间</param>
private void CreateCookieValue(string cookieName,string cookieValue,DateTime cookieTime)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value=cookieValue;
//DateTime dtNow = DateTime.Now ;
//TimeSpan tsMinute = cookieTime;
cookie.Expires = cookieTime;
Response.Cookies.Add(cookie);
}
///<summary>
///创建cookie值
///</summary>
///<param name="cookieName">cookie名称</param>
///<param name="cookieValue">cookie值</param>
///<param name="subCookieName">子信息cookie名称</param>
///<param name="subCookieValue">子信息cookie值</param>
///<param name="cookieTime">cookie有效时间</param>
private void CreateCookieValue(string cookieName,string cookieValue,string subCookieName,string subCookieValue,DateTime cookieTime)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value=cookieValue;
cookie[subCookieName]=subCookieValue;
cookie.Expires = cookieTime;
Response.Cookies.Add(cookie);
}
///<summary>
///取得cookie的值
///</summary>
///<param name="cookieName">cookie名称</param>
///<returns></returns>
private string GetCookieValue(string cookieName)
{
string cookieValue="";
HttpCookie cookie = Request.Cookies[cookieName];
if(null == cookie)
{
cookieValue="";
}
else
{
cookieValue=cookie.Value;
}
return cookieValue;
}
///<summary>
///取得cookie的值
///</summary>
///<param name="cookieName">cookie名称</param>
///<param name="subCookieName">cookie子信息值</param>
///<returns></returns>
public static string GetCookieValue(string cookieName, string subCookieName)
{
string cookieValue = "";
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (null == cookie)
{
cookieValue = "";
}
else
{
cookieValue = cookie.Value;
string[] words = cookieValue.Split('&');
foreach (string s in words)
{
if (s.IndexOf(subCookieName+"=") >= 0)
{
cookieValue = s.Split('=')[1];
}
}
// cookieValue = cookieValue.Split('&')[1].ToString().Split('=')[1];
}
return cookieValue;
}
///<summary>
///删除某个固定的cookie值[此方法一是在原有的cookie上再创建同样的cookie值,但是时间是过期的时间]
///</summary>
///<param name="cookieName"></param>
private void RemoteCookieValue(string cookieName)
{
string dt="1900-01-01 12:00:00";
CreateCookieValue(cookieName,"",Convert.ToDateTime(dt));
}
#endregion
以下是调用:
private void Page_Load(object sender, System.EventArgs e)
{
// 在这里放置使用者程序代码以初始化网页
this.txt_UserID.Text=this.GetCookieValue("UserName","UserID");//取得用户名
}
private void btn_Submit_Click(object sender, System.EventArgs e)
{
#region用Cookie进行保存登入用户名
if(this.chb_IsSave.Checked)
{
//将用户保存一个小时,具体设置可以进行调整。。
//这里用了固定的公用的cookie用户UserName,用户编号UserID进行访问
CreateCookieValue("UserName","UserName","UserID",this.txt_UserID.Text,DateTime.Now+new TimeSpan(0,1,0,0));//设置保存用户名
}
#endregion
}
///<summary>
///创建cookie值
///</summary>
///<param name="cookieName">cookie名称</param>
///<param name="cookieValue">cookie值</param>
///<param name="cookieTime">cookie有效时间</param>
private void CreateCookieValue(string cookieName,string cookieValue,DateTime cookieTime)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value=cookieValue;
//DateTime dtNow = DateTime.Now ;
//TimeSpan tsMinute = cookieTime;
cookie.Expires = cookieTime;
Response.Cookies.Add(cookie);
}
///<summary>
///创建cookie值
///</summary>
///<param name="cookieName">cookie名称</param>
///<param name="cookieValue">cookie值</param>
///<param name="subCookieName">子信息cookie名称</param>
///<param name="subCookieValue">子信息cookie值</param>
///<param name="cookieTime">cookie有效时间</param>
private void CreateCookieValue(string cookieName,string cookieValue,string subCookieName,string subCookieValue,DateTime cookieTime)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Value=cookieValue;
cookie[subCookieName]=subCookieValue;
cookie.Expires = cookieTime;
Response.Cookies.Add(cookie);
}
///<summary>
///取得cookie的值
///</summary>
///<param name="cookieName">cookie名称</param>
///<returns></returns>
private string GetCookieValue(string cookieName)
{
string cookieValue="";
HttpCookie cookie = Request.Cookies[cookieName];
if(null == cookie)
{
cookieValue="";
}
else
{
cookieValue=cookie.Value;
}
return cookieValue;
}
///<summary>
///取得cookie的值
///</summary>
///<param name="cookieName">cookie名称</param>
///<param name="subCookieName">cookie子信息值</param>
///<returns></returns>
public static string GetCookieValue(string cookieName, string subCookieName)
{
string cookieValue = "";
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (null == cookie)
{
cookieValue = "";
}
else
{
cookieValue = cookie.Value;
string[] words = cookieValue.Split('&');
foreach (string s in words)
{
if (s.IndexOf(subCookieName+"=") >= 0)
{
cookieValue = s.Split('=')[1];
}
}
// cookieValue = cookieValue.Split('&')[1].ToString().Split('=')[1];
}
return cookieValue;
}
///<summary>
///删除某个固定的cookie值[此方法一是在原有的cookie上再创建同样的cookie值,但是时间是过期的时间]
///</summary>
///<param name="cookieName"></param>
private void RemoteCookieValue(string cookieName)
{
string dt="1900-01-01 12:00:00";
CreateCookieValue(cookieName,"",Convert.ToDateTime(dt));
}
#endregion
以下是调用:
private void Page_Load(object sender, System.EventArgs e)
{
// 在这里放置使用者程序代码以初始化网页
this.txt_UserID.Text=this.GetCookieValue("UserName","UserID");//取得用户名
}
private void btn_Submit_Click(object sender, System.EventArgs e)
{
#region用Cookie进行保存登入用户名
if(this.chb_IsSave.Checked)
{
//将用户保存一个小时,具体设置可以进行调整。。
//这里用了固定的公用的cookie用户UserName,用户编号UserID进行访问
CreateCookieValue("UserName","UserName","UserID",this.txt_UserID.Text,DateTime.Now+new TimeSpan(0,1,0,0));//设置保存用户名
}
#endregion
}