SQL 服务器可信连接在 IIS 中有错误的用户,但 SSMS 正常

SQL Server trusted connection has wrong user in IIS, but SSMS is OK

我正在使用 VS 2012,使用 windows 凭据连接到 SQL Server 2008 主机。我可以从 Management Studio 正常连接,但是当我 运行 一个 Web 应用程序(IIS 7.5 windows 7 ASP.NET 4.0 集成)和我的应用程序尝试连接时,我得到这个:

Login failed for user 'MyDomain\MyMachineName$'.

出于某种原因,使用网络身份的 IIS 应用程序池没有使用 MyUserName,而是使用我的机器名。我的连接字符串是:

<connectionStrings>
    <clear />
    <add name="Assignment" 
         connectionString="Data Source=a.b.c.d;Initial Catalog=My_Db;Trusted_Connection=true;Max Pool Size=200;Pooling=True;" />
</connectionStrings>

我应该提到这是一台新的(对我来说)电脑。我查看了凭证管理器,那里什么也没有。知道为什么 IIS 和我的 Web 应用程序使用了错误的东西吗?

当您 运行 您的应用程序池在网络服务身份下时,IIS 实际上正在做它设计的事情。

Using the Network Service account in a domain environment has a great benefit. Worker process running as Network Service access the network as the machine account. Machine accounts are generated when a machine is joined to a domain. They look like this:

<domainname>\<machinename>$,

For example: mydomain\machine1$

The nice thing about this is that network resources like file shares or SQL Server databases can be ACLed to allow this machine account access.

以上引用来自以下文档...

http://www.iis.net/learn/manage/configuring-security/application-pool-identities

希望这能回答为什么它没有按您预期的方式工作的问题。

至于如何让它按照你想要的方式工作,我认为这篇文章应该回答这个问题,如果没有,一些基本的 google 搜索应该会提供相当多的信息来说明如何为您要执行的操作正确配置您的环境。

https://gilesey.wordpress.com/2013/05/11/allowing-iis-7-5-applications-to-communicate-to-sql-server-via-windows-authentication/