I will show you how easy it is to add a "remember me on next login" feature so people is automatically logged in when visiting your site in the future.
First just add a checkbox (I havn't renamed it in this sample, just calling it CheckBox1) in your login form:

Then in your code handler for the button click:
private void btnLogin_Click(object sender, System.EventArgs e)
{
if ( FormsAuthentication.Authenticate(txtID.Text, txtPwd.Text) )
{ //It went well FormsAuthentication.RedirectFromLoginPage(txtID.Text,CheckBox1.Checked);
}
else
{
LabelError.Text = "Error logging in";
}
}
So the last parameter to RedirectFromLoginPage is CheckBox1.Checked which will then handle everything from us.
Under the hood it is of course solved by setting a cookie, but that's nothing we have to worry about. Sometimes you just gotta love ASP.NET !
本文介绍如何轻松地在网站登录表单中添加“记住我”功能,以便用户下次访问时能够自动登录。通过简单的代码示例展示了如何使用ASP.NET中的FormsAuthentication类实现这一功能。
1365

被折叠的 条评论
为什么被折叠?



