How can I tell what the username is of an authenticated using Forms Authentication with ASP.NET
After a user is authenticated I store their username in session state but if the session times out, I want to create a new session for the user based on their username they authenticated with original. How can I get from Forms Auth开发者_JAVA技巧entication the currently authenticated user?
The current authenticated user should be the name in the IIdentity
assigned to the identity of the IPrincipal
on the User
property of the HttpContext
HttpContext.Current.User.Identity.Name
In ASP.NET MVC, it is available in a controller via
this.User.Identity.Name
You don't need to store the user name in session at all - in your page simply access the User property of HttpContext. To get the actual username you would use User.Identity.Name
, and as a handy short cut the ASP.NET Page class itself has a user property, so you could do
string userName = Page.User.Identity.Name;
in your code behind.
If you're using ASP.NET MVC there's a User property you can access in a controller
string userName = User.Identity.Name
精彩评论