我正在开发一个连接到数据库(在另一台机器上)的 ASP.NET 应用程序。当我尝试从我的计算机进行连接时,没有问题,当我将其部署到目标环境中时,EF 会返回异常:
The underlying provider failed on Open.System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'NT AUTHORITYANONYMOUS LOGON'. at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at
我的配置是:
<authentication mode="Windows" />
<customErrors mode="Off"></customErrors>
<identity impersonate="true" />
<security>
<authentication>
<windowsAuthentication useKernelMode="false">
<extendedProtection tokenChecking="None" />
</windowsAuthentication>
</authentication>
</security>
我的连接字符串是:
<add name="pmgbicopEntities" connectionString="metadata=res://*/copDB.csdl|res://*/copDB.ssdl|res://*/copDB.msl;provider=System.Data.SqlClient;provider connection string="Data Source=[NAME]devde;Initial Catalog=[NAME];Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
如果我删除数据库指令,则此。Page.User 是我当前的帐户(这是正确的!),但是当我通过 EF 打开新连接时,我变得"匿名"
我尝试了这样的冒充:
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
ctx = winId.Impersonate();
//EF SOMETHING
}
catch (Exception exx)
{
}
finally
{
if (ctx != null)
ctx.Undo();
}
但是我收到相同的异常"用户'NT权限\匿名登录'登录失败"。
我尝试更改应用程序池用户,尝试手动模拟,但没有任何成功。
IIS 配置有一些我尝试启用和禁用的 kerberos 内容,但没有任何变化。
你有什么建议吗?我是否必须在机器之间启用"信任"之类的东西?对于我的"假IIS"开发机器,我没有问题,但我的用户在数据库上已启用。
提前谢谢你。
您的最终目标是什么:将用户的凭据传递给 SQL 或使用预定帐户连接到 SQL?
以下是我必须配置 Kerberos 才能工作的设置(它允许您使用用户的帐户权限):
IIS 设置:
-Physical Path Credentials: Application User (pass-through authentication)
-Physical Path Credential Logon Type: Clear Text
-Application Pool ID: DomainServiceAccount
-Integrated Windows Authentication: Enabled
-Windows Authentication Providers: Negotiate
-useAppPoolCredentials (found in Configuration Editor): True
为服务帐户创建的 SPN:SETSPN -L Domain\ServiceAccount
HTTP/Our.WebSiteURL.com
如果不想使用域服务帐户,请在 SPN 和应用程序池中将其替换为域\计算机帐户。