I'm trying to get windows user name in ASP.NET Core 3.0
I have found here many questions on this topic, but I cannot find any answer I could use.
My app uses login-password authentication, but lot of users (not all of them) has own windows login, so I would like to give them opportunity to log-in via Windows login.
On the localhost works this code:
string windowsUserName = Environment.UserDomainName + "\\" + Environment.UserName;
but when I deploy it to server then I get this username: IIS APPPOOL/DefaultAppPool
I have tried to change launchsettings from:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
to:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
but I'm able to get the windows login name only via HttpContext.User.Identity.Name (I have tried this only on localhost), but this method I don't want to use - I'm storing here Id of the user from database, so I don't want to rewrite it.
Is there any chance to get required data without using windows login for everybody?
Many thanks for suggestions.
make sure your web.config as below:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore forwardWindowsAuthToken="true" processPath="C:\Program Files\dotnet\dotnet.exe" arguments=".\YourWebsite.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
<security>
<authentication>
<windowsAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</configuration>
HomeController.cs You can then get a user Windows username from a controller using the below code.
public IActionResult Index()
{
string userName = HttpContext.User.Identity.Name;
return View(userName);
}
Update:
which you mentioned in your post which is called mixed-mode authentication. you can not achieve this by using a single application you need to use two applications to implement your requirement. refer this link for more detail how can you achieve this.