【问题标题】:Windows Authentication problems in IISIIS 中的 Windows 身份验证问题
【发布时间】:2021-07-11 06:05:38
【问题描述】:

我正在使用 C# 和 ASP.NET 制作具有集成安全性的基于 Web 的数据库应用程序,以便使用 Windows 用户访问数据库。当我从 Visual Studio 在 IIS Express 中运行应用程序时,一切都很好。当我在 IIS 下发布网页时,我遇到了 Windows 身份验证问题。

在 IIS Express 中,以下两行代码生成了与当前登录用户对应的相同 Windows 用户名,这也是我想要的:

string user = Page.User.Identity.Name;
string loginName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

但在 IIS 中发布的表单中,第二行给出了不同的用户:IIS APPPOOL\"the-name-of-my-Visual-Studio-solution"。此用户对数据库没有权限,应用程序崩溃。

我在 web.config 中启用了 Windows 身份验证:

<authentication mode="Windows" />
    <identity impersonate ="false" />
    <authorization>
      <deny users="?" />
    </authorization>

在 IIS 中,使用以下设置(见下图):

IIS authentication settings

这是应用程序池设置:

IIS Application pools settings

到哪里寻找问题?

【问题讨论】:

  • 您希望它获取哪些用户凭据? IIS 在它自己的用户下启动它,这是合乎逻辑的。只需授予该用户对数据库的访问权限,一切都会好起来的。
  • 是的,这是可能的。但在我们的环境中,Windows 用户还可以控制您在 SQL Server 中可以访问哪些数据库。我希望 Page.User.Identity 与 WindowsIdentity 相同。
  • blog.lextudio.com/… 第一次使用 IIS 的用户必须了解这些差异。

标签: c# asp.net windows authentication iis


【解决方案1】:

试试这些代码:

string UserName = HttpContext.Current.User.Identity.Name;
string UserName = Request.LogonUserIdentity.Name;

【讨论】:

    【解决方案2】:

    问题与模仿有关。为了进行正确的身份验证,我必须做两件事:1) 我必须在 IIS 中为我​​的应用程序启用 ASP.NET 模拟。 (这也可以在 web.config 中完成,但我不希望这样做,因为我必须更改服务器配置才能使其工作。) 2)我必须从 Integrated 更改应用程序池的托管管道模式到 Classic,这也在 IIS 中,用于我的应用程序。

    然后它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多