0

Using Forms Authentication, I am storing a cookie for each user if they mark Remember Me during login, using the following piece of code in the Login1_LoggedIn event.

if (rememberMe.Checked) FormsAuthentication.SetAuthCookie(Login1.UserName, true);

When the user arrives on my page with a cookie, I need to get his/her user name so I can check their roles. Does the Forms Authentication cookie store this information, and how can I retrieve it?

2 Answers 2

2

The string you parse to SetAuthCookie (Login1.UserName in your case) will be stored in the IPrincipal when the user accesses a page. You can access it using:

Page.User.Identity.Name
Sign up to request clarification or add additional context in comments.

3 Comments

The problem with this is that when I check for the cookie, the user is not logged in; therefore his identity is not established, and consequently I cannot use this code.
Haha. Ignore my last comment. Silly me for jumping to conclusions. +1 and Accepted for DaveG!.
It's also possible to store user roles in the authentication cookie itself and use Page.User.IsInRole("yourrole");
1

Try

Page.User.Identity.Name

or

HttpContext.Current.User.Identity.Name

1 Comment

+1 cause this is correct, but DaveG beat ya so he gets accepted. Still, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.