【问题标题】:Asp.net Core Web API - Current user & Windows AuthenticationAsp.net Core Web API - 当前用户和 Windows 身份验证
【发布时间】:2017-06-28 21:51:41
【问题描述】:

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

现在我们需要在创建/编辑给定项目(即在核心 API 中)期间将登录用户的用户名存储在表中。

我们已经尝试过

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

我在使用 Visual Studio 时使用 WindowsIdentity 获得用户名,但使用 IIS 时,它提供的值是 Asp.Netcore,即池名称

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

使用 IIS 6.1 版

我错过了什么吗?

【问题讨论】:

  • 我有同样的问题,但 iis 7.5 在生产中。在我的开发机器上,我得到了我的 ntId,但是当我部署到 prod 时,我仍然得到应用程序池的名称。部署时没有launchSettings.json 文件。您在部署到生产环境时是否遇到同样的问题?
  • 更新 - 我将 System.Security.Principal.WindowsIdentity.GetCurrent().Name 更改为 User.Identity.Name,现在它可以工作了。我以前没有工作过。

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

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

    • forwardWindowsAuthToken - 是的,之前尝试过,但问题没有解决,Daboul 也提出了同样的建议
    • launchSettings.json,设置windowsAuthentication: true & anonymousAuthentication: 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>
    

    【讨论】:

    • 在我的情况下,情况是相反的,我的 launchsettings.json 文件是正确的,但是在两个地方都设置了 anonymousAuthentication = false 后,IIS 设置没有按照给定的答案,它对我有用。感谢您节省我的时间:-)
    • 您是如何使用具有价值的 user.identity 在 Web API 上实现 Windows 身份验证的。我正在努力实现与 lit-html 相同的前端,它访问 API 以获取数据并且无法将值获取到 user.identity。
    【解决方案3】:

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

    1. 转到 IIS 中的 Web 应用程序
    2. 打开配置编辑器
    3. 将部分更改为 system.webServer/serverRuntime
    4. 将 authenticatedUserOverride 更改为 UseAuthenticatedUser(对我来说,它设置为 UseWorkerProcessUser)

    更多详情请参考以下链接; https://blogs.iis.net/jaroslad/what-does-the-authenticateduseroverrideuser-do

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-22
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2018-01-28
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多