James Alexander
James Alexander

Reputation: 6312

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

Upvotes: 0

Views: 115

Answers (2)

blowdart
blowdart

Reputation: 56550

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

Upvotes: 1

Russ Cam
Russ Cam

Reputation: 125538

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

Upvotes: 2

Related Questions