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

时间:2017-02-10 14:00:37

标签: iis asp.net-core windows-authentication

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

现在,我们需要在创建/编辑期间为表格中的用户存储用户名,例如在核心API中。

我们尝试过

  • WindowsIdentity.GetCurrent()。名称,它给出了IIS APPPOOL \ Asp.netCore
  • HttpContext.User.Identity给出空值

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

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

使用IIS版本6.1

我错过了什么吗?

3 个答案:

答案 0 :(得分:5)

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

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>

答案 1 :(得分:4)

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

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

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

  • forwardWidnowsAuthToken - 是的,之前已尝试过,但问题没有解决,Daboul建议同样如此
  • launchSettings.json,Set&#34; windowsAuthentication&#34;:true&amp; &#34; anonymousAuthentication&#34;:false

执行此操作后,我能够在User.Identity对象中进行值。

launchSettings.json文件:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false
    }
}

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>

答案 2 :(得分:0)

在Windows Server 2012 R2 / IIS 8.0上,即使在web.config中设置forwardWindowsAuthToken = true后,User.Identity.Name也没有返回用户名,而是返回IIS APPPOOL以解决我在下面做的更改问题;

  1. 转到IIS中的Web应用程序
  2. 打开配置编辑器
  3. 将Section更改为system.webServer / serverRuntime
  4. 将authenticatedUserOverride更改为UseAuthenticatedUser(对我来说,它设置为UseWorkerProcessUser)
  5. 有关详细信息,请参阅以下链接; https://blogs.iis.net/jaroslad/what-does-the-authenticateduseroverrideuser-do

相关问题