菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
499
0

登录时记住用户名和密码及cookie案例应用

原创
05/13 14:22
阅读数 77329

 

记住这些信息,可以使用Cookie来实现,更多Cookie应用,可参考
http://jb51.net/article/33590.htm
http://jb51.net/article/33591.htm
现在我们来模拟一个登录介面:

复制代码 代码如下:

<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>


 

我们要判断用户在点铵钮的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 = DateT编程客栈ime.Now.AddDays(7);http://www.cppcns.com
}

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"].Valuwww.cppcns.come;
}
}
}





本文标题: 登录时记住用户名和密码及cookie案例应用
本文地址: http://www.cppcns.com/wangluo/aspnet/91773.html

发表评论

0/200
499 点赞
0 评论
收藏
为你推荐 换一批