更改 Web.Config 后登录失败

Login fails after changing Web.Config

更改 web.config 后,出现如下错误。

变更前:

<connectionStrings>
    <add name="CS1"
         connectionString="Server=S1; Database=DB1; User ID=Admin; Password=AdminPW; Persist Security Info=True; Max Pool Size=200; Connection Timeout=60" />
</connectionStrings>

变更后:

<connectionStrings>
    <add name="CS1" 
         connectionString="Data Source=S1; Initial Catalog=DB1; Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

错误:

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

堆栈跟踪:

[SqlException (0x80131904): Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.]
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) +1431
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +1085
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +70
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +964
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +109
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +1529
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +156
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +258
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +312
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +202
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +413
   System.Data.SqlClient.SqlConnection.Open() +128
   MROWebPartCollection.BLL.Common.DataProvider.ConnectionDB() +263
   MROWebPartCollection.BLL.Provider.MROQ_TBL_USER_PROFILEProvider..ctor() +245
   MROWebPartCollection.ProductWP.CoaCoc.CoaCocUserControl..ctor() +125
   ASP._controltemplates_mrowebpartcollection_productwp_coacoc_coacocusercontrol_ascx..ctor() +16
   __ASP.FastObjectFactory_app_web_coacocusercontrol_ascx_16352c62_o2rjt3li.Create_ASP__controltemplates_mrowebpartcollection_productwp_coacoc_coacocusercontrol_ascx() +31
   System.Web.UI.TemplateControl.LoadControl(IWebObjectFactory objectFactory, VirtualPath virtualPath, Type t, Object[] parameters) +282
   System.Web.UI.TemplateControl.LoadControl(VirtualPath virtualPath) +120
   MROWebPartCollection.ProductWP.CoaCoc.CoaCoc.CreateChildControls() +96
   System.Web.UI.Control.EnsureChildControls() +130
   System.Web.UI.Control.PreRenderRecursiveInternal() +66
   System.Web.UI.Control.PreRenderRecursiveInternal() +276
   System.Web.UI.Control.PreRenderRecursiveInternal() +276
   System.Web.UI.Control.PreRenderRecursiveInternal() +276
   System.Web.UI.Control.PreRenderRecursiveInternal() +276
   System.Web.UI.Control.PreRenderRecursiveInternal() +276
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6881

如果您使用的是集成安全性,则必须授予应用程序池活动目录帐户访问您的 SQL 服务器数据库的权限。这里有一些提示:

  1. 您使用的帐户必须是在网络服务器和数据库服务器上均有效的活动目录帐户。如果您在同一台机器上,本地帐户也可以使用。
  2. 为了给您的网站分配身份,请在 IIS 中找到应用程序池(应用程序池下的站点设置),然后在应用程序池设置中找到应用程序池身份 属性。将应用程序池标识设置为您选择的活动目录帐户。
  3. 在 SQL 服务器中,创建一个映射到您的活动目录帐户的 SQL 用户
  4. 创建用户后,您可以授予该用户访问您的数据库的权限

当然,每一步都有很多细节需要注意。

我还应该注意,上面的所有说明都假定您要使用标准登录来设置数据库。另一方面,如果您想使用正在浏览您站点的用户的 Active Directory 凭据通过 SQL 服务器进行身份验证,则您需要使用 IIS 模拟而不是上述步骤。 This article 解释模拟的基础知识。