Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Forms Authentication with UserData Problem

Reply
Thread Tools

Forms Authentication with UserData Problem

 
 
=?Utf-8?B?SGFyZHkgV2FuZw==?=
Guest
Posts: n/a
 
      02-15-2007
Hi all,
I am running ASP.NET 2.0, after login I need to pass CustomerID in my
database instead of username to other pages. I added following code to my
login.aspx

protected void Login_Authenticate(object sender, AuthenticateEventArgs e) {
//FormsAuthentication.SignOut();
if (Membership.ValidateUser(Login.UserName, Login.Password)) {
int customerID = GetCustomerIDByUsername(Login.UserName);
if (customerID > 0) {
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
Login.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(60),
Login.RememberMeSet,
customerID.ToString(),
FormsAuthentication.FormsCookiePath);

// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);

// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
encTicket));

e.Authenticated = true;
} else {
e.Authenticated = false;
}
} else {
e.Authenticated = false;
}
}

Then I have another page to read this cookie, FormsIdentity identity =
Context.User.Identity as FormsIdentity; I set a break point at this line, and
find out the cookie version is "2" instead of "1" I set in login.aspx. And I
cannot read my userData from cookie, it turns to be blank.

Anybody has idea what is wrong?

Thanks!
--
Regards
Hardy
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-16-2007
Hi there,

Login control does the same thing internally (passing String.Empty as user
defined data), please look at the exact code which is executed internally:

private void AttemptLogin()
{
if ((this.Page == null) || this.Page.IsValid)
{
LoginCancelEventArgs args1 = new LoginCancelEventArgs();
this.OnLoggingIn(args1);
if (!args1.Cancel)
{
AuthenticateEventArgs args2 = new AuthenticateEventArgs();
this.OnAuthenticate(args2);
if (args2.Authenticated)
{
FormsAuthentication.SetAuthCookie(
this.UserNameInternal, this.RememberMeSet);
this.OnLoggedIn(EventArgs.Empty);
this.Page.Response.Redirect(
this.GetRedirectUrl(), false);
}
else
{
//...
}
}
}
}

Because you're not redirecting after setting authentication cookie, login
control creates another cookie, that overwrites created one (version 2).
Provided code does the same thing so in theory you could redirect to request
page after cookie with custom data has been set:

// amended code you provided
// Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
encTicket));
Response.Redirect(this.GetRedirectUrl(), true);

Beware current thread will be aborted, so you won't receive any events
(Login1_LoggedIn, page unload). Otherwise, it is not possible to attach user
data to form authentication cookie (of course when using login control)
without unpacking the ticket in Login.LoggedIn event handler, appending the
custom data and reissuing authentication cookie.

Hope this helps
--
Milosz


"Hardy Wang" wrote:

> Hi all,
> I am running ASP.NET 2.0, after login I need to pass CustomerID in my
> database instead of username to other pages. I added following code to my
> login.aspx
>
> protected void Login_Authenticate(object sender, AuthenticateEventArgs e) {
> //FormsAuthentication.SignOut();
> if (Membership.ValidateUser(Login.UserName, Login.Password)) {
> int customerID = GetCustomerIDByUsername(Login.UserName);
> if (customerID > 0) {
> FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
> Login.UserName,
> DateTime.Now,
> DateTime.Now.AddMinutes(60),
> Login.RememberMeSet,
> customerID.ToString(),
> FormsAuthentication.FormsCookiePath);
>
> // Encrypt the ticket.
> string encTicket = FormsAuthentication.Encrypt(ticket);
>
> // Create the cookie.
> Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
> encTicket));
>
> e.Authenticated = true;
> } else {
> e.Authenticated = false;
> }
> } else {
> e.Authenticated = false;
> }
> }
>
> Then I have another page to read this cookie, FormsIdentity identity =
> Context.User.Identity as FormsIdentity; I set a break point at this line, and
> find out the cookie version is "2" instead of "1" I set in login.aspx. And I
> cannot read my userData from cookie, it turns to be blank.
>
> Anybody has idea what is wrong?
>
> Thanks!
> --
> Regards
> Hardy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set userdata in auth ticket and still support cookieless authentication? Peter Rilling ASP .Net 1 08-03-2006 03:45 PM
Authentication Ticket not storing UserData Sean Patterson ASP .Net 0 01-25-2005 12:08 AM
Forms Authentication UserData Brian Shannon ASP .Net 1 08-16-2004 07:21 PM
forms authentication ticket .userdata vanishing e ASP .Net 1 10-24-2003 06:14 PM
Cannot retrieve UserData in Forms Authentication John Kievlan ASP .Net Security 1 07-25-2003 12:51 AM



Advertisments