登錄時記住用戶名和密碼及cookie案例應(yīng)用
登錄樣子,可以參考某一論壇的登錄介面:
記住這些信息,可以使用Cookie來實(shí)現(xiàn),更多Cookie應(yīng)用,可參考
http://jb51.net/article/33590.htm
http://jb51.net/article/33591.htm
現(xiàn)在我們來模擬一個登錄介面:
<table>
<tr>
<td style="width: 15%; text-align: right;">
User Name
</td>
<td>
<asp:TextBox ID="TextBoxUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right;">
Password
</td>
<td>
<asp:TextBox ID="TextBoxPassword" TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td style="text-align: right;">
Remember me
</td>
<td>
<asp:CheckBox ID="CheckBoxRememberMe" runat="server" />
</td>
</tr>
<tr>
<td style="text-align: right;">
</td>
<td>
<asp:Button ID="ButtonLogin" runat="server" Text="Login" OnClick="ButtonLogin_Click" />
</td>
</tr>
</table>
運(yùn)行時的效果:

我們要判斷用戶在點(diǎn)銨鈕的Click事件時,是否有選擇Remember me這個CheckBox,如果選中了,要把這個登錄的信息記錄至Cookie,還要把Cookie的過期時間設(shè)置7天之后過期。反之,只把登錄的信息記錄入Cookie之中,不設(shè)置Cookie的過期時間??梢詤⒖枷旅娴牡卿浭录a:
protected void ButtonLogin_Click(object sender, EventArgs e)
{
Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
if (CheckBoxRememberMe.Checked)
{
Response.Cookies["Name"].Expires = DateTime.Now.AddDays(7);
Response.Cookies["Password"].Expires = DateTime.Now.AddDays(7);
}
Response.Cookies["Name"].Value = this.TextBoxUserName.Text.Trim();
Response.Cookies["Password"].Value = this.TextBoxPassword.Text.Trim ();
}
接下來,你還得在Page_load中去讀取Cookie.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["Name"] != null && Request.Cookies["Password"] != null)
{
this.TextBoxUserName.Text = Request.Cookies["Name"].Value;
this.TextBoxPassword.Attributes["value"] = Request.Cookies["Password"].Value;
}
}
}
看看操作演示,演示中有三種狀態(tài)演示,第一種是沒有點(diǎn)選CheckBox,這樣的話,關(guān)閉窗口,下次再打開時,沒有記住登錄的信息。
第二是點(diǎn)選擇了CheckBox,這樣下次再打開窗口,還可以看到帳號與密碼存儲在相應(yīng)的文本框中,這都是Cookie沒有過期。
第三種,再點(diǎn)一次登錄,沒有點(diǎn)選remember me的CheckBox,這樣系統(tǒng)又移除了Cookie:
相關(guān)文章
asp.net使用DataGridTree實(shí)現(xiàn)下拉樹的方法
這篇文章主要介紹了asp.net使用DataGridTree實(shí)現(xiàn)下拉樹的方法,詳細(xì)的講述了DataGridTree實(shí)現(xiàn)下拉樹的原理與具體實(shí)現(xiàn)方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11DataGridView使用自定義控件實(shí)現(xiàn)簡單分頁功能(推薦)
這篇文章主要介紹了DataGridView使用自定義控件實(shí)現(xiàn)簡單分頁功能,數(shù)據(jù)庫使用的是sqlserver,本文通過通過實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友參考下吧2019-11-11ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫建模
這篇文章介紹了ASP.NET?Core使用EF為關(guān)系數(shù)據(jù)庫建模的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04asp.net Http異常eurl.axd出錯信息解決方法
在IIS6中同時啟用了ASP.NET 2.0 和 ASP.NET 4.0 后,網(wǎng)站程序可能會出現(xiàn)如下錯誤:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-08-08Visual Studio Debug實(shí)戰(zhàn)教程之?dāng)帱c(diǎn)操作
眾所周知斷點(diǎn)對于Visual Studio調(diào)試過程是十分重要的,斷點(diǎn)的設(shè)置也是為了更好的進(jìn)行調(diào)試。下面這篇文章主要給大家介紹了關(guān)于Visual Studio Debug實(shí)戰(zhàn)教程之?dāng)帱c(diǎn)操作的相關(guān)資料,需要的朋友可以參考下2018-09-09asp.net后臺cs中的JSON格式變量在前臺Js中調(diào)用方法(前后臺示例代碼)
本文主要介紹下asp.net后臺cs中的JSON格式變量在前臺Js中調(diào)用方法,下面是前后臺的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈,下對大家有所幫助2013-06-06asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
這篇文章介紹了asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼,有需要的朋友可以參考一下2013-09-09