An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

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

Monday, April 25, 2005 7:50 AM
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.

Feedback

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

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

Neat Blog. Will visit again.

-Rajeev 7/22/2005 9:45 PM | Rajeev Narang

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

Thanks, It saved my time.
10/18/2005 11:25 AM | Mandar

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

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

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

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

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

Thank you very much, excellent stuff!! 1/15/2006 8:08 PM | Plamen

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

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

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

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

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

Nice, just what I was looking for. Thanks. 11/27/2006 7:44 PM | BT

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

great help! 8/15/2007 3:31 AM | rafa arg

Post a comment





 

Please add 2 and 6 and type the answer here: