Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is HttpContext.User.Identity set?

Tags:

I have authentication code:

var authTicket = new FormsAuthenticationTicket(/*blahblah....*/); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName,                              FormsAuthentication.Encrypt(authTicket)); Response.Cookies.Add(cookie); var name = HttpContext.User.Identity.Name; // line 4 

By putting in debug statements, I find that name on line 4 is empty. But the next time I make a call on this browser session, the HttpContext.User.Identity.Name is correctly set.

So when does this value get set?

like image 382
Shaul Behr Avatar asked Jul 06 '11 10:07

Shaul Behr


People also ask

How is HttpContext current user identity Name set?

It just holds the username of the user that is currently logged in. After login successful authentication, the username is automatically stored by login authentication system to "HttpContext.Current.User.Identity.Name" property.

How do you set HttpContext user identity for an application manually?

You can achieve this by manually settings HttpContext. User: var identity = new ClaimsIdentity("Custom"); HttpContext. User = new ClaimsPrincipal(identity);

How do I find HttpContext current user identity Name?

Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an . aspx page without using the fully qualified class reference to HttpContext. For example, you can use User.Identity.Name to get the name of the user on whose behalf the current process is running.

Where is HttpContext user stored?

It is most likely stored in Managed Passwords: Click Start > Run. Enter "control userpasswords2"


1 Answers

The HttpContext.User.Identity.Name will be set if the given Request contains the authentication cookie. In your case the cookie has just been added to the Response for the Browser to pick up. The Browser will add the cookie on the following requests if it exists.

like image 111
tehshin Avatar answered Jan 04 '23 16:01

tehshin