Redirect not working

L

Ldraw

I have looked at all the Redirect questions on this site without finding a
resoultion
to my redirect problem. I am using sample code to verify a user and
password from a login page but although the verification is successful and I
can see that the return url is where I need to go I am continously looped
back to the login page.
Cookies are enabled. See code example below.

--WebConfig--
<authentication mode="Forms">
<forms name="TestLoginAthu" path="/" loginUrl="WebForm1.aspx"
protection="All" timeout="30">
<credentials passwordFormat="Clear">
<user name="jeff" password="test" />
<user name="mike" password="test" />
</credentials>
</forms>
</authentication>

--Code Behind

HttpCookie cookie = FormsAuthentication.GetAuthCookie ( TextBox1.Text,
CheckBox1.Checked );
// Expires in 30 days, 12 hours and 30 minutes from today.
cookie.Expires = DateTime.Now.Add(new TimeSpan(30, 12, 30, 0));
Response.Cookies.Add (cookie);
string strUrl = FormsAuthentication.GetRedirectUrl ( TextBox1.Text,
CheckBox1.Checked );
Response.Redirect( strUrl );
 
M

Mark Miller

As far as I can tell you have protection set to "All" which uses encryption
for the cookie. But you are not ecrypting the cookie before you write it to
the Response stream. I'm not sure how to encrypt a cookie after calling
GetAuthCookie() but an alternative is to create a FormsAuthentication ticket
and then use the FormsAuthentication.Encrypt() method passing in the ticket.
Your sample it should give you the exact same functionality you need. Here's
a sample:

//create ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(30), true,
string.Empty);
//encrypt ticket and capture string result
string sEncryptedTicket = FormsAuthentication.Encrypt( ticket );
//create a new cookie and add it to the Response stream
Response.Cookies.Add( new System.Web.HttpCookie(
FormsAuthentication.FormsCookieName, sEncryptedTicket ) );

I hope that helps.

Regards,
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top