Remember me on next login

This is a small enhancement made to the samples in these articles

Built-in Forms Authentication – description

Built-in Forms Authentication – sample

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 !

Leave a Response