Reputation: 1977
I'm trying to get current windows user in new ASP.Net 5.
This returns correctly when run from Visual Studio, when I deploy it to the server it returns app pool identity:
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
This returns empty string:
string name = System.Threading.Thread.CurrentPrincipal.Identity.Name;
This have been dropped in bew ASP version:
HttpContext.Current.User.Identity
Any help much appreciated.
Upvotes: 1
Views: 1254
Reputation: 4563
What is the authentication mode are you using?
If you have following settings
Then your code below should give "name" as domain\WinAccount Name
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Upvotes: 1
Reputation: 2116
You get the app pool identity because that is the account with which your process executes. If you want the identity of the user making the call you need to use impersonation.
You can either add <identity impersonate="true" />
to web.config or you can avoid direct editing of web.config by using IIS Manager; select your web site, click Authentication and enable ASP.NET Impersonation.
Upvotes: 0
Reputation: 30618
You've not specified where you want to obtain this information, but from within a Controller it is available as User.Identity.Name
:
public IActionResult Index()
{
ViewData["Message"] = $"Hello, {User.Identity.Name}";
return View();
}
This results in "Hello, Desktop-PC\User". You can also do this within a View.
Upvotes: 1