Asp.net核心Web API - 当前用户和Windows身份验证

Man*_*sar 7 iis windows-authentication asp.net-core

我们在应用程序AngularJS2 Asp.Net Core API SQL Server中有以下技术堆栈

现在,我们需要在创建/编辑期间为表中的用户存储用户名,即给定项目,即在Core API中.

我们尝试过

  • WindowsIdentity.GetCurrent().Name,它给出了IIS APPPOOL\Asp.netCore
  • HttpContext.User.Identity给出null值

我在使用Visual Studio时使用WindowsIdentity获取用户名,但是使用IIS时,它会将值作为Asp.Netcore即池名称

启用Windows身份验证并禁用匿名身份验证

使用IIS V6.1

我错过了什么吗?

Dab*_*oul 6

您是否在web.config中将forwardWindowsAuthToken设置为true?

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>
Run Code Online (Sandbox Code Playgroud)


Man*_*sar 5

我环顾四周,建议使用Windows身份验证创建Asp.Net Core WebApi应用程序。

因此,当我使用Windows身份验证创建Asp.Net Core WebApi时,它起作用了,并且在User.Identity对象中获得了值。

所以我创建了2个应用程序,即一个带有Windows身份验证的应用程序,一个没有Windows身份验证的应用程序,然后比较了所有文件并发现以下文件中的更改

  • forwardWindowsAuthToken -是的,之前曾尝试过,但问题并未解决,Daboul提出了同样的建议
  • launchSettings.json,设置为 windowsAuthentication:true&anonymousAuthentication:false

完成此操作后,我就可以在User.Identity对象中进行赋值了。


launchSettings.json文件:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false
    }
}
Run Code Online (Sandbox Code Playgroud)

Web.Config:

<?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>
Run Code Online (Sandbox Code Playgroud)