Forms Authentication - Redirecting users to a Page other than Default.aspx

In this article I will explain how to redirect users to a specific page rather than the generic default.aspx upon successful authentication of the user.

While using ASP.NET Forms authentication, if we try to access a protected page, the user would be taken to the login.aspx page with the ReturnUrl parameter having the path for the originally requested page.

Once, the user's credentials are verified, the RedirectFromLoginPage method can be used to take the user back to the originally requested page.

However, if there is no specified ReturnUrl, then FormsAuthentication by default takes the user to the default.aspx page upon successful authentication.

If we do not have a default.aspx page or we want to take the users to our custom page etc., then we can use the Setauthcookie method to set the cookie and then redirect users to our desired page. The following code establishes the same.

// Once the user's entered credentials are verified //
if(Request.Params["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage(txtUserName.text, false);
}
else
{
FormsAuthentication.SetAuthcookie(txtUserName.text, false);
Response.Redirect("CustomPage.aspx");
}

The above code first verifies whether there is any ReturnUrl parameter such that if exists, it should take to the originally requested page.

Else, it sets the authcookie and then redirects user to a custom page.

The txtUserName is the ID of the textbox which is used to capture the username.

This article applies to ASP.NET 1.0 & 1.1 Versions.

Print | posted on Monday, April 25, 2005 7:50 AM

Comments on this post

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thanks for the sharing this info. I found it very useful.

Neat Blog. Will visit again.

-Rajeev
Left by Rajeev Narang on Jul 22, 2005 9:45 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thanks, It saved my time.
Left by Mandar on Oct 18, 2005 11:25 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thanks load for the blog, this one had me stumped. I'm glad someone has taken to the time to explain. Cheers!!
Left by Cameron Dempster on Nov 13, 2005 7:06 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thanks much!! save me a couple of hours.
Left by Brian B. on Jan 08, 2006 3:31 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thank you very much, excellent stuff!!
Left by Plamen on Jan 15, 2006 8:08 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thanks, I was stuck on this problem for a while, mostly because I didn't know about the ReturnUrl parameter. This explanation is great: short and to the point.
Left by OlivierB on Aug 02, 2006 11:45 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
If your using .Net 2.0 or 3.0 you can just catch the "LoggedIn" event and redirect from there.

Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
If Request.Item("ReturnURL") = "" Then
Response.Redirect("tasclimb_default.aspx")
Else
Response.Redirect(HttpUtility.UrlDecode(Request.Item("ReturnURL")))
End If
End Sub

Just means you don't have to worry about calling any of the login methods.
Left by Rowan on Nov 09, 2006 3:21 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Nice, just what I was looking for. Thanks.
Left by BT on Nov 27, 2006 7:44 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
great help!
Left by rafa arg on Aug 15, 2007 3:31 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
cool, thanks man
Left by budiman on Oct 29, 2008 12:31 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
thnaks super blog azdiricilar.net
Left by azdırıcı on Nov 30, 2008 4:40 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
I know this is a bit old now but, anyone got an idea how to fix missing query parameters?

For instance, if my returnurl is ReturnURL=mainpage.aspx?a=b&c=d

Response.Redirect(HttpUtility.UrlDecode(Request.Item("ReturnURL"))) will redirect me to mainpage.aspx?a=b (missing off the c=d bit).

Any help appreciated
Left by Stu on Jan 30, 2009 6:11 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Actually .. figured it out ..

string returnstring = Request["ReturnURL"];
for (int a = 1; a < Request.QueryString.Count; a++ )
returnstring += "&" + Request.QueryString.GetKey(a) + "=" + Request.QueryString.Get(a);

FormsAuthentication.SetAuthCookie(this.UserName.Text, true);
Response.Redirect(returnstring);

Can't help but feel it's a bit of a hack though. If anyone can come up with a "cleaner" way, it'd be appreciated.

Cheers,

Stu
Left by Stu on Jan 30, 2009 6:29 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Really very nice post. Exactly what i needed.
Thanks a lot
Left by Kpant on Jul 14, 2009 6:57 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
nice post.thanks admin.
Left by mario oyunları on Sep 21, 2009 10:39 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thank U so much!!!!.. i been looking fot this the whole morning!!
and definitely the best solution =)
Left by K on Jan 27, 2010 7:00 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thank you very much for this post. Thanks, mig33
Left by Free Stuff on May 15, 2010 3:59 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Couldnt say anything better. you captured it all.
Left by S. Oluoch on May 23, 2010 1:02 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
This realy saved my time and it is very usefull simple and smart tip.

Thanks,
Left by Franklin on Jun 23, 2010 9:36 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Really useful.Thanks
Left by Prasanna on Nov 10, 2010 1:55 PM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Thanks ..this worked for me..
Left by uttam on Oct 11, 2011 12:52 AM

# re: Forms Authentication - Redirecting users to a Page other than Default.aspx

Requesting Gravatar...
Tahanks.......this is working for me .............
Left by Ravi G on Jun 02, 2014 2:39 PM

Your comment:

 (will show your gravatar)