views:

974

answers:

1

Dear All, I have one application where after successful Login user will be redirected to Home.aspx. Now if I try Response.Redirect("Home.aspx") it doesnt work, But if I try FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, false);..its working. Now my question is why Response.Redirect() is not working? I know FormsAuthentication.RedirectFromLoginPage do much more than Login, it also sets cookie,and also redirects to Login Page, but why Redirct() is not working? web.config:

<authentication mode="Forms">
  <forms loginUrl="LogIn.aspx" defaultUrl="Home.aspx" path="/"></forms>
</authentication>
<authorization>
  <deny users="?"/>
</authorization>

Can somebody help?

+4  A: 

You already have the answer pretty much.

Response.Redirect does not set the authentication cookie so when Home.aspx is loading it fails authentication and will redirect you back to the login page.

To use response.redirect, you will have to manage the cookie yourself, an example from http://www.4guysfromrolla.com/webtech/110701-1.3.shtml is:

Dim cookie As HttpCookie = FormsAuthentication.GetAuthCookie(UserName.Text, _
                        chkPersistCookie.Checked)
Response.Cookies.Add (cookie)
Response.Redirect(FormsAuthentication.GetRedirectUrl (UserName.Text, _
                        chkPersistCookie.Checked))

EDIT:

To answer the question in your comment, if you pass true as the second parameter to RedirectFromLoginPage then the cookie will be set to never expire, and you won't need to login again.

FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, true)
Patrick McDonald
Thanks Patrick for ur help and the link :-).I have one question, lets say after successful login I have closed the window.So next time when I start browsing I should directly be redirectrd to Home.aspx rather than Login.aspx(same as stackoverflow).Now how to implement this.Can u put some example or link.
Wondering
It's working like a magic.Thanks.
Wondering
Won't FormsAuthentication.SetAuthCookie help here?
Charles Prakash Dasari