欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

登錄時(shí)記住用戶名和密碼及cookie案例應(yīng)用

 更新時(shí)間:2013年01月27日 12:48:25   作者:  
本文將實(shí)現(xiàn)登錄時(shí)記住用戶的帳號(hào)密碼,接下來(lái)我們來(lái)模擬一個(gè)登錄介面,要把這個(gè)登錄的信息記錄至Cookie,還要把Cookie的過(guò)期時(shí)間設(shè)置7天之后過(guò)期,感興趣的朋友可以參考下,希望本文對(duì)你的cookie學(xué)習(xí)有所幫助

登錄樣子,可以參考某一論壇的登錄介面:

 

記住這些信息,可以使用Cookie來(lái)實(shí)現(xiàn),更多Cookie應(yīng)用,可參考
http://jb51.net/article/33590.htm
http://jb51.net/article/33591.htm
現(xiàn)在我們來(lái)模擬一個(gè)登錄介面:

復(fù)制代碼 代碼如下:

<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)行時(shí)的效果:

 

我們要判斷用戶在點(diǎn)銨鈕的Click事件時(shí),是否有選擇Remember me這個(gè)CheckBox,如果選中了,要把這個(gè)登錄的信息記錄至Cookie,還要把Cookie的過(guò)期時(shí)間設(shè)置7天之后過(guò)期。反之,只把登錄的信息記錄入Cookie之中,不設(shè)置Cookie的過(guò)期時(shí)間。可以參考下面的登錄事件代碼:

復(fù)制代碼 代碼如下:

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 ();
}

接下來(lái),你還得在Page_load中去讀取Cookie.
復(fù)制代碼 代碼如下:

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)演示,第一種是沒(méi)有點(diǎn)選CheckBox,這樣的話,關(guān)閉窗口,下次再打開(kāi)時(shí),沒(méi)有記住登錄的信息。

第二是點(diǎn)選擇了CheckBox,這樣下次再打開(kāi)窗口,還可以看到帳號(hào)與密碼存儲(chǔ)在相應(yīng)的文本框中,這都是Cookie沒(méi)有過(guò)期。

第三種,再點(diǎn)一次登錄,沒(méi)有點(diǎn)選remember me的CheckBox,這樣系統(tǒng)又移除了Cookie:

相關(guān)文章

最新評(píng)論