473,473 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Forms Authentication with UserData Problem

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
Feb 15 '07 #1
1 7055
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
Feb 16 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bill yeager | last post by:
Everything is working in my authentication process except for the fact that I can't retrieve the "UserData" property from the "FormsAuthenticationTicket". Write before I do a...
11
by: VB Programmer | last post by:
PLEASE HELP.... I'm having trouble. In my login form after I've verified the username/password are valid I do this: Select Case iMyPrivilege Case 0 Dim arrRoles() As String = {"guest"}...
1
by: e | last post by:
I'm using forms authentication on a site. When the user logs in via the login page, the entered creds are checked against AD, and if valid, an encrypted forms authentication ticket is produced and...
3
by: Martin | last post by:
Dear fellow ASP.NET programmer, I stared using forms authentication and temporarily used a <credentials> tag in web.config. After I got it working I realized this wasn't really practical. I...
5
by: Kenneth Keeley | last post by:
Hi, I have a web app that has forms authentication and I can login to the page the first time I go there but it never times me out if I come back in 24 hours a hit the refresh key the page loads...
2
by: Ed | last post by:
Hi I currently have an asp.NET project. I'm using Access 2003 and forms authentication to authenticate users. Can anyone tell me how to set the roles in asp.NET so that it recognizes them? The...
3
by: Mike | last post by:
I have a web application that the forms authentication cookie is not expiring correctly. When I look at the trace information of a newly requested page after the session and forms authentication have...
1
by: Brian Shannon | last post by:
Using forms authentication I want to add several pieces of data to the UserData property in the Forms Ticket. I want to include roles, email, user ID. From reading it seems like you add all...
0
by: Sean Patterson | last post by:
Hey all, I've followed the examples online on how to use Forms Authentication to create a ticket, assign it a role, and then intercept it in the Global.asax file to make sure it gets sucked in...
3
by: chuck rudolph | last post by:
Folks, Can anyone confirm that my understading is correct and maybe shed some light on why it's as it is. (I'm guessing security, but that seems weak to me.) The asp.net web application is using...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.