Framework: asp.net core 2.2 I'm trying to get the Windows logged in user. I get it when running on the localhost, but when deploying the application to the IIS- I'm getting the application pool name.
After investigating it, I figure out I need to setup a web.config for the deploy, along with the launchsettings for the developent env.
This is the relevant controller code:
[Route("getUser")]
[HttpGet]
public IActionResult GetUser()
{
var currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
return Ok(currentUser.Name);
}
This is how iisSettings were initially defined in the launchSettings, and it worked perfectly on my localhost (iisexpress):
"iisSettings":{
"windowsAuthentication": false,
"anonymousAuthentication": true
...
}
When tried to set it up to enable windowsAuthentication:
"iisSettings":{
"windowsAuthentication": true,
"anonymousAuthentication": false
...
}
I got 401.2 unauthorized http error code.
**Second version additional data:
So I'm using the first edition of the launch settings ("windowsAuthentication": false, "anonymousAuthentication": true) for development env. - All worked well in the localhost.
I then added a web.config file, in order to setup the IIS for hosting after deploy. This is how my web.config looks like, more or less (free handed copying, not cut+paste):
<system.web>
<authentication mode="Windows"></authentication>
<identity impersonate="false" /> //This is because I'm getting the username by code
</system.web>
<system.webServer>
<aspnetCore processPath=....... forwardWindowsAuthToken="true" hsotingModel="InProcess">
</aspnetCore>
<security>
<authentication>
<anonymousAuthentication enabled ="true" />
<windowsAuthentication enabled ="true" />
</authentication>
</security>
</system.webServer>
In that stage I even cannot start up the localhost, I'm getting config error:
"This configuration section cannot be used at this path. This happens when the section is locked at aparent level..."
The authentication section is causing the error, meaning - when I comment it out the error disappear. What should I do ?