2
votes

I have the following code in my asp.net login page:

if (Request.QueryString["ReturnUrl"] != null)
        FormsAuthentication.RedirectFromLoginPage(UserLogin.UserName, UserLogin.RememberMeSet);
    else
        FormsAuthentication.SetAuthCookie(UserLogin.UserName, UserLogin.RememberMeSet);

The scenario I want is:

when the user enters the login page, it will be checked if he has an authentication cookie, and if so, he is automatically redirected to the default page (which is a page that only authenticated users can see).

How can this be achieved ?

2

2 Answers

3
votes

Place this in the Page_Init for example...

  if (Request.IsAuthenticated) {
            Response.Redirect(Request.QueryString["ReturnUrl"]);
  }

It'll just bounce the user onwards to the destination if they're logged in.

3
votes

If the authentication cookie is present and it is valid, the context will be populated with the user data. Just check then if:

public class Login_Page {
   public void Page_Load( ... ) {
      if ( this.Context.User != null && this.Context.User.Identity != null &&
           this.Context.User.Identity.IsAuthenticated )
        this.Response.Redirect( FormsAuthentication.DefaultUrl );
      }
   }