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.

posted @ Monday, April 25, 2005 7:50 AM

Print

Comments on this entry:

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

Left by Rajeev Narang at 7/22/2005 9:45 PM
Gravatar
Thanks for the sharing this info. I found it very useful.

Neat Blog. Will visit again.

-Rajeev

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

Left by Mandar at 10/18/2005 11:25 AM
Gravatar
Thanks, It saved my time.

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

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

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

Left by Brian B. at 1/8/2006 3:31 PM
Gravatar
Thanks much!! save me a couple of hours.

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

Left by Plamen at 1/15/2006 8:08 PM
Gravatar
Thank you very much, excellent stuff!!

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

Left by OlivierB at 8/2/2006 11:45 AM
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.

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

Left by Rowan at 11/9/2006 3:21 AM
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.

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

Left by BT at 11/27/2006 7:44 PM
Gravatar
Nice, just what I was looking for. Thanks.

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

Left by rafa arg at 8/15/2007 3:31 AM
Gravatar
great help!

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

Left by budiman at 10/29/2008 12:31 PM
Gravatar
cool, thanks man

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

Left by azdırıcı at 11/30/2008 4:40 PM
Gravatar
thnaks super blog azdiricilar.net

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

Left by Stu at 1/30/2009 6:11 PM
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

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

Left by Stu at 1/30/2009 6:29 PM
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

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

Left by Kpant at 7/14/2009 6:57 AM
Gravatar
Really very nice post. Exactly what i needed.
Thanks a lot

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

Left by mario oyunları at 9/21/2009 10:39 AM
Gravatar
nice post.thanks admin.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345