Using forms authentication for the first time

W

William Gower

I have set up my web config file to use Forms Authentication. In my login
page after I validate my user, I want to store the id but I don't want to
call the RedirectFromLoginPage. I want to redirect the page myself. How do
I store the id without calling RedirectFromLoginPage?
 
H

Hernan de Lahitte

You should create the ticket by your self. Here is a sample code:

Place this code instead of "FormsAuthentication.RedirectFromLoginPage(
UserId.Text, false )"

HttpCookie cookie = FormsAuthentication.GetAuthCookie( UserId.Text, false );
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
cookie.Value );
// Store roles inside the Forms cookie.
FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(
ticket.Version,

ticket.Name,

ticket.IssueDate,

ticket.Expiration,

ticket.IsPersistent,

userData,

ticket.CookiePath );
cookie.Value = FormsAuthentication.Encrypt(newticket);
Response.Cookies.Set(cookie);
Response.Redirect( FormsAuthentication.GetRedirectUrl( newticket.Name,
newticket.IsPersistent ) );

In case you don't have any userData (some string data up to 1200 bytes
aprox) to store inside the ticket, you might use this
FormsAuthenticationTicket overload:

FormsAuthenticationTicket newticket2 = new FormsAuthenticationTicket(
ticket.Name, ticket.IsPersistent, ticket.Expiration.Subtract(
ticket.IssueDate ).Minutes );


Hope this help.

--
Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hernan de Lahitte

Agreed with Raterus.
Something like:

FormsAuthentication.SetAuthCookie( UserId.Text, false );
Response.Redirect( "yourPage.aspx" );

should be ok.


--
Hernan de Lahitte
Lagash Systems S.A.
http://weblogs.asp.net/hernandl


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top