现在的位置: 首页 > 综合 > 正文

在login控件中加入”remember me on next login”

2013年10月13日 ⁄ 综合 ⁄ 共 789字 ⁄ 字号 评论关闭

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 !

 

抱歉!评论已关闭.