How to Override ReturnURL in ASP.NET

Written by kamal on Jun 17, 2007
Change the default ReturnURL Programatically in ASP .NET 2.0

Explanation:

If you are uing FormsAuthentication and a user who is not logged in tries to enter a secured page he is redirected to the Login page and the URL is appended with a ReturnUrl. After the user has been authenticated, the user is redirected to the earlier requested page.

I had no problem with this feature until I timed out and hit my LogOff page. I wasn't authenticated to see the LogOff page, so it appended that page URL to the ReturnURL and sent me the LogOn page. Once I logged in, it redirected me back to the LogOff page, which promptly logged me out.

I decided it would be easier to pick the start page for the user, regardless of what the ReturnUrl parameter was. Instead of using FormsAuthentication.RedirectFromLoginPage, use FormsAuthentication.SetAuthCookie and handle the Redirect yourself. 

if (FormsAuthentication.Authenticate(txtName.Text, txtPassword.Text))
{
    FormsAuthentication.SetAuthCookie(txtName.Text, true);
    Response.Redirect("YourDefaultSecuredStartPage.aspx", true);             
}

In above code user will be checked for authentication and will be returned to the menaul secured page which you want. If you are developing a Community based web page better location for FIRST redirect would be the user Home/Profile.

Visitors/Readers Comments
(for questions please use The Forum)



kumar
But how do i alter ReturnUrl without using Response.Redirect???

21/05/2008 05:07:25 UTC

murthy
If you are uing FormsAuthentication and a user who is not logged in tries to enter a secured page he is redirected to the Login page and the URL is appended with a ReturnUrl. After the user has been authenticated, the user is redirected to the earlier requested page.

I had no problem with this feature until I timed out and hit my LogOff page. I wasn't authenticated to see the LogOff page, so it appended that page URL to the ReturnURL and sent me the LogOn page. Once I logged in, it redirected me back to the LogOff page, which promptly logged me out.

I decided it would be easier to pick the start page for the user, regardless of what the ReturnUrl parameter was. Instead of using FormsAuthentication.RedirectFromLoginPage, use FormsAuthentication.SetAuthCookie and handle the Redirect yourself.

04/09/2008 02:09:44 UTC

Gary Mason
I am having the same sort of problem.

When connecting to my site it redirects me to the login.aspx page from the default.aspx page using forms authentication. I enter my login details and it takes me back to the default page. In the default page i have redirects, in the onload procedure, to a user page depending on the login details used. 

When i logout it takes me back to the default.aspx page but fills in the returnurl as the user page i just left. if i then login with another username it then tries to take me back to the previous users page and doesn't even trigger the redirect for the logged in user. Basically when I logout i want to be taken back to the dfault page with no return url at this point it should then send me to the login page with a returnurl=default.aspx. This would then trigger the onload event.

Any ideas?

15/10/2008 04:52:37 UTC




Add your Comments

Name:  
Message:


Advertise Here
For more details
Contact Us

Advertisments