Monday, September 18, 2006

Page Redirect After Login in Forms Authentication

Normally, we will be redirected to the originally requesting page, with ReturnUrl query string appended in the URL after successful user login in Form Authentication using FormsAuthentication.RedirectFromLoginPage() method. Otherwise, the we will be redirected to default.aspx, by default.

The question is, what method do we need to use if we want to redirect the users to different page other than default.aspx? Here I demonstrate how it can be accomplished.


bool isAuthenticated = true;

if(isAuthenticated) // after user is authenticated
{
   if(Request.Params["ReturnUrl"] != null)
   {
      FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false);
   }
   else
   {
      FormsAuthentication.SetAuthcookie(txtUsername.Text, false);
      Response.Redirect("yourpage.aspx");
   }
}


Get rid of default ones.... !

1 comment:

Unknown said...

Oops lower case c in SetAuthcookie() :-)