Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current user who's accessing an ASP.NET application?

To get the current logged in user at the system I use this code:

string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); 

I work on an ASP.NET application where I need this information. So I've put my application on a server and tried the code above, and I get "Network Service" in the string opl. I need to know the current user of the PC who accesses my ASP.NET application.

like image 230
Tassisto Avatar asked Mar 24 '11 09:03

Tassisto


People also ask

How do I get the current UserName in .NET using C #?

GetCurrent(). Name; Returns: NetworkName\Username.

How can get current user details in ASP.NET core?

You can create a method to get the current user : private Task<ApplicationUser> GetCurrentUserAsync() => _userManager. GetUserAsync(HttpContext. User);

What is HttpContext current user identity?

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.


1 Answers

The quick answer is User = System.Web.HttpContext.Current.User

Ensure your web.config has the following authentication element.

<configuration>     <system.web>         <authentication mode="Windows" />         <authorization>             <deny users="?"/>         </authorization>     </system.web> </configuration> 

Further Reading: Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application

like image 173
BenCr Avatar answered Sep 19 '22 20:09

BenCr