views:

36

answers:

2

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 Authentication the currently authenticated user?

+1  A: 

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
blowdart
How about from a MVC Controller?
James Alexander
+2  A: 

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
Russ Cam
That's what I was looking for. Thanks.
James Alexander