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?
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.
You can achieve this by manually settings HttpContext. User: var identity = new ClaimsIdentity("Custom"); HttpContext. User = new ClaimsPrincipal(identity);
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.
It is most likely stored in Managed Passwords: Click Start > Run. Enter "control userpasswords2"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With