为什么 asp.net 核心应用程序在连接到 SQL Server 时使用与 AppPool 身份不同的用户进行 Windows 身份验证?

Lie*_*ero 3 sql-server iis windows-authentication asp.net-core

我已将 asp.net core 2.2 web 应用程序作为网站部署到 IIS。我已将应用程序池标识设置为 ApplicationPoolIdentity:

[IIS 屏幕截图[1]

我可以看到System.Security.Principal.WindowsIdentity.GetCurrent()返回

IIS APPPOOL\MyCustomAppPool
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用连接字符串连接到 SQL Server 时:

Server=localhost;Database=MyDadatabse;Trusted_Connection=True;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误(忽略德语):

SqlException: Fehler bei der Anmeldung für den Benutzer "CompanyDomain\ComputerName$"。

为什么不使用 ApplicationPool 标识?

另请注意,当我将自定义用户名/密码设置为应用程序池的身份时,它会起作用。

Dav*_*oft 5

为了澄清这一点, APPPOOL\MyCustomAppPool像 LocalServer、NetworkService 和 per-service SID 一样,都是本地身份。它们不存在于域中,因此不能用于访问远程资源。然而,服务器本身在域上有一个帐户,当以这些身份运行的代码访问远程资源时,它们使用计算机帐户。

如果您在 IIS 服务器上有多个应用程序池标识,并且需要区分它们对网络资源的访问权限,或者防止计算机帐户的权限累积,则需要为应用程序池配置域帐户。

如果您使用应用程序池标识连接到本地 SQL Server,则错误消息将(令人困惑地)声称您正在使用计算机帐户进行连接。这是 SQL Server 如何报告错误的缺陷。例如,如果我尝试从 IIS 应用程序连接到本地 SQL Server,它将失败,

Login failed for user 'MyDomain\MyServer$'
Run Code Online (Sandbox Code Playgroud)

直到我授予对本地 SQL Sever 的访问权限

create login [IIS APPPOOL\DefaultAppPool] from windows
create user [IIS APPPOOL\DefaultAppPool] for login [IIS APPPOOL\DefaultAppPool]
grant select to [IIS APPPOOL\DefaultAppPool]
Run Code Online (Sandbox Code Playgroud)