How To Auto-login Using ASP.Net FormsAuthentication

For our demo website, demo.quickpm.net, I need to have a default automatic login. Using the ASP.Net Membership and FormsAuthentication an easy way to do auto-login is the following code

string userEmail = "john.doe@gmail.com";
string password = "thepassword";
if (Membership.ValidateUser(userEmail, password))
{
      FormsAuthentication.SetAuthCookie(userEmail, true);
      Response.Redirect("~/");
}

The above code assumes that “john.doe@gmail.com” is a user and “thepassword” is their password.

Advertisements

2 thoughts on “How To Auto-login Using ASP.Net FormsAuthentication

  1. I found that if I insert FormsAuthentication.Authenticate(userEmail, password) after FormsAuthentication.SetAuthCookie(userEmail, true), it works.

    1 string userEmail = “john.doe@gmail.com”;
    2 string password = “thepassword”;
    3 if (Membership.ValidateUser(userEmail, password))
    4 {
    5 FormsAuthentication.SetAuthCookie(userEmail, true);
    FormsAuthentication.Authenticate(userEmail, password);
    6 Response.Redirect(“~/”);
    7 }

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s