當前位置:成語大全網 - 書法字典 - 如何讀取Cookie中的值,以便下次登錄時不用再次輸入用戶名和密碼?

如何讀取Cookie中的值,以便下次登錄時不用再次輸入用戶名和密碼?

blogs . com/over 140/archive/2008/11/19/1336497 . html

ASP.NET cookie的簡單應用[ASP.NET | cookie]

壹般有會員和用戶機制的網站或論壇,登錄時都會有這樣壹個復選框——【記住我的名字|兩周不要登錄|把我的信息保存在這臺電腦上】,語句很多,實現也差不多。本文做壹個簡單的例子,並附帶壹個C# Cookies幫助類CookiesHelper.cs和壹段代碼片段,希望對初學者有所幫助:)

推薦文章

1.林新亮index.dat文件分析;深入分析了cookies在index.dat中的存儲。

2.MSDN-ASP.NET曲奇概述全面詳細地介紹了曲奇。

主體

CookiesHelper.cs

使用系統;

使用系統。集合。泛型;

使用系統。文本;

使用系統。Web

命名空間庫。對象助手

{

公共類CookiesHelper

{

#區域獲取Cookie

///& lt;總結& gt

///獲取Cookie的值

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;returns & gt& lt/returns & gt;

公共靜態字符串GetCookieValue(字符串cookieName)

{

返回GetCookieValue(cookieName,null);

}

///& lt;總結& gt

///獲取Cookie的值

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;returns & gt& lt/returns & gt;

公共靜態字符串GetCookieValue(字符串cookieName,字符串鍵)

{

request = HttpContext。當前。請求;

如果(請求!=空)

返回GetCookieValue(請求。Cookies[cookieName],key);

返回“”;

}

///& lt;總結& gt

///獲取Cookie的子項值

///& lt;/summary & gt;

///& lt;param name = " cookie " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;returns & gt& lt/returns & gt;

公共靜態字符串GetCookieValue(http cookie cookie,字符串鍵)

{

如果(cookie!=空)

{

如果(!字符串。IsNullOrEmpty(key)& amp;& amp餅幹。HasKeys)

返回cookie。值[鍵];

其他

返回cookie。價值;

}

返回“”;

}

///& lt;總結& gt

///獲取Cookie

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;returns & gt& lt/returns & gt;

公共靜態http cookie get cookie(string cookieName)

{

request = HttpContext。當前。請求;

如果(請求!=空)

退貨請求。cookie[cookieName];

返回null

}

#結束區域

#區域刪除Cookie

///& lt;總結& gt

///刪除Cookie

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

公共靜態void remove cookie(string cookieName)

{

RemoveCookie(cookieName,null);

}

///& lt;總結& gt

///刪除Cookie的子項

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

公共靜態void remove cookie(string cookieName,string key)

{

HttpResponse響應= HttpContext。當前。響應;

如果(回應!=空)

{

HttpCookie cookie = response。cookie[cookieName];

如果(cookie!=空)

{

如果(!字符串。IsNullOrEmpty(key)& amp;& amp餅幹。HasKeys)

餅幹。Values.Remove(鍵);

其他

回應。cookies . Remove(cookieName);

}

}

}

#結束區域

#區域設置/修改Cookie

///& lt;總結& gt

///設置Cookie子項的值

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

公共靜態void SetCookie(字符串cookieName,字符串key,字符串value)

{

SetCookie(cookieName,key,value,null);

}

///& lt;總結& gt

///設置Cookie值

///& lt;/summary & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

公共靜態void SetCookie(字符串鍵,字符串值)

{

SetCookie(key,null,value,null);

}

///& lt;總結& gt

///設置Cookie值和過期時間。

///& lt;/summary & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

///& lt;param name = " expires " & gt& lt/param & gt;

公共靜態void SetCookie(字符串鍵,字符串值,日期時間過期)

{

SetCookie(key,null,value,expires);

}

///& lt;總結& gt

///設置Cookie過期時間

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " expires " & gt& lt/param & gt;

公共靜態void SetCookie(字符串cookieName,日期時間過期)

{

SetCookie(cookieName,null,null,expires);

}

///& lt;總結& gt

///設置Cookie

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

///& lt;param name = " expires " & gt& lt/param & gt;

公共靜態void SetCookie(string cookieName,string key,string value,DateTime?過期)

{

HttpResponse響應= HttpContext。當前。響應;

如果(回應!=空)

{

HttpCookie cookie = response。cookie[cookieName];

如果(cookie!=空)

{

如果(!字符串。IsNullOrEmpty(key)& amp;& amp餅幹。HasKeys)

餅幹。Values.Set(key,value);

其他

如果(!字符串。IsNullOrEmpty(值))

餅幹。值=值;

如果(過期!=空)

餅幹。過期=過期。價值;

回應。set cookie(cookie);

}

}

}

#結束區域

#區域添加Cookie

///& lt;總結& gt

///添加Cookie

///& lt;/summary & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

公共靜態void AddCookie(字符串鍵,字符串值)

{

AddCookie(new HttpCookie(key,value));

}

///& lt;總結& gt

///添加Cookie

///& lt;/summary & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

///& lt;param name = " expires " & gt& lt/param & gt;

公共靜態void AddCookie(字符串鍵,字符串值,日期時間過期)

{

http cookie cookie = new http cookie(key,value);

餅幹。Expires =過期;

add cookie(cookie);

}

///& lt;總結& gt

///添加為Cookie。值集合

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

公共靜態void AddCookie(字符串cookieName,字符串key,字符串value)

{

http cookie cookie = new http cookie(cookieName);

餅幹。Values.Add(key,value);

add cookie(cookie);

}

///& lt;總結& gt

///添加為Cookie集合

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gtCookie名稱

///& lt;param name = " expires " & gt到期時間

公共靜態void AddCookie(字符串cookieName,日期時間過期)

{

http cookie cookie = new http cookie(cookieName);

餅幹。Expires =過期;

add cookie(cookie);

}

///& lt;總結& gt

///添加為Cookie。值集合

///& lt;/summary & gt;

///& lt;param name = " cookieName " & gt& lt/param & gt;

///& lt;param name = " key " & gt& lt/param & gt;

///& lt;param name = " value " & gt& lt/param & gt;

///& lt;param name = " expires " & gt& lt/param & gt;

公共靜態void AddCookie(字符串cookieName,字符串key,字符串value,日期時間過期)

{

http cookie cookie = new http cookie(cookieName);

餅幹。Expires =過期;

餅幹。Values.Add(key,value);

add cookie(cookie);

}

///& lt;總結& gt

///添加Cookie

///& lt;/summary & gt;

///& lt;param name = " cookie " & gt& lt/param & gt;

公共靜態void AddCookie(HttpCookie cookie)

{

HttpResponse響應= HttpContext。當前。響應;

如果(回應!=空)

{

//指定客戶端腳本是否可以訪問[默認值為false]

餅幹。HttpOnly = true

//指定統壹的路徑,這將啟用通用訪問。

餅幹。路徑= "/";

//設置跨域,以便在其他二級域名下訪問。

//cookie。Domain = " chinesecoo.com

回應。append cookie(cookie);

}

}

#結束區域

}

}

代碼註意事項:

1.添加cookie時,請註意壹個cookie可以添加多個子項。

2.如果不顯示設置HttpCookie。過期,那麽這個cookie的生存期就是瀏覽器進程-關閉瀏覽器將會過期。

3.妳需要知道HttpCookie的屬性。域和HttpCookie。路徑,對於跨域、存儲路徑等高級應用很有幫助。

讓我們看看實際的調用代碼

頁面加載

受保護的void Page_Load(對象發送方,EventArgs e)

{

如果(!IsPostBack)

{

HttpCookie Cookie = CookiesHelper。get cookie(" UserInfo ");

如果(Cookie!=空)

{

this.txtUserName.Value = Cookie。values[" uName "];

}

}

}

代碼描述:

1.this.txtUserName是壹個在界面上輸入用戶名的控件,改成自己的控件名就可以了;

2.UserInfo是Cookie的名稱,uName是Cookie的子項。

這段代碼的功能是在頁面加載時顯示用戶Cookie中記錄的用戶名。在這裏也可以和密碼壹起記錄然後顯示在這裏,讓用戶直接點擊登錄按鈕。

登錄驗證

string uName = this . txtusername . value . trim();

//驗證用戶名和密碼

if (IsLogin(uName,this.txtPass.Value.Trim()))

{

//記住我的名字

如果(!字符串。IsNullOrEmpty(請求["remUsername"]))

{

HttpCookie Cookie = CookiesHelper。get cookie(" UserInfo ");

if (Cookie == null)

{

cookie = new http cookie(" UserInfo ");

餅幹。Values.Add("uName ",uName);

//設置Cookie過期時間

餅幹。Expires =日期時間。now . AddDays(365);

CookiesHelper助手。add Cookie(Cookie);

}

else if(!餅幹。值["uName"]。等於(uName))

CookiesHelper助手。SetCookie("UserInfo "," uName ",uName);

}

回應。重定向(" ~/index . aspx ");

}

代碼描述:

1.this.txtUserName和this.txtPass是界面上輸入用戶名和密碼的控件,改成自己的控件名即可;

2.IsLogin方法用於驗證用戶名和密碼是否正確。換成妳自己的驗證方法就好了。

3.請求["remUsername"]是為了獲取界面上的[記住我的名字]復選框是否被選中。

這段代碼的作用是在用戶登錄成功後,將用戶名保存在客戶端的Cookie中,並將過期時間設置為1年。

結束

我在發這篇文章的時候猶豫了。說實話,我基本上是通過查MSDN得到的,但從別人的字典到我自己的字典還是有區別的。好好學習,多查查MSDN:)

標簽標簽:ASP.NET餅幹,ASP。NET實現,記住用戶名,把我的信息保存在本機上。