登录样子,可以参考某一论坛的登录介面:
记住这些信息,可以使用Cookie来实现,更多Cookie应用,可参考
现在我们来模拟一个登录介面:
复制代码 代码如下:
<table>
<tr>
<td>
User Name
</td>
<td>
<asp:TextBox runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox TextMode="Password" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Remember me
</td>
<td>
<asp:CheckBox runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button runat="server" Text="Login" />
</td>
</tr>
</table>
我们要判断用户在点铵钮的Click事件时,是否有选择Remember me这个CheckBox,如果选中了,要把这个登录的信息记录至Cookie,还要把Cookie的过期时间设置7天之后过期。反之,只把登录的信息记录入Cookie之中,不设置Cookie的过期时间。可以参考下面的登录事件代码:
复制代码 代码如下:
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 ();
}
复制代码 代码如下:
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;
}
}
}
新闻热点
疑难解答
图片精选