1

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.

Lukas
  • 83
  • 1
  • 9
  • "My app uses login-password authentication", then you can modify your app code to allow them to fill in Windows account names and passwords to log in, and leave IIS settings untouched. Your description of "localhost" is probably wrong when you actually runs on VS/IIS Express. There are fundamental differences, https://blog.lextudio.com/web-application-differences-in-visual-studio-and-iis-60fec7e311b3 – Lex Li Jul 13 '20 at 13:51

1 Answers1

0

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.

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • Thank you for suggestion, but as I wrote in question - I'm using HttpContext.User.Identity.Name for ID of user within our company - and this is user in whole project - so to change it as Lex Li for instance suggested does not make sense. Second thing is, that when I disable anonymousAuthentication then username-login fails... – Lukas Jul 15 '20 at 03:58