如何修复System.Data.SqlClient.SqlException:“用户“ xx \ xxxx $”登录失败”

时间:2019-04-23 04:54:50

标签: sql-server entity-framework iis asp.net-web-api

我在IIS上运行了一个Web应用程序,并且获得了

System.Data.SqlClient.SqlException: "Login failed for user'domain\account$'".

当调用函数 DbMigrator.Update()时,这是一个例外情况

private readonly DbMigrationsConfiguration<MyContext> _config;
public ContextInitializer(string connectionString)
{
    _config = new Configuration()
    {
        TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient")
    };
}
...

public void InitializeDatabase(MgmtStudioContext context)
{
    var migrator = new DbMigrator(_config);
    migrator.Update();
}

这是堆栈跟踪:

at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)\r\n   
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)\r\n  
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)\r\n  
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)\r\n   
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)\r\n   
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)\r\n   
at System.Data.SqlClient.SqlConnection.Open()\r\n   
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)\r\n   
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()\r\n   
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()\r\n   
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)\r\n   
at System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)\r\n   
at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)\r\n   
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)\r\n   
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)\r\n   at 

这是我的连接字符串:

Data Source=(local);Initial Catalog=myTable; Integrated Security=True;

该代码在其他计算机上也可以正常工作,但在我的计算机上不起作用,并且我无法找到根本原因。

2 个答案:

答案 0 :(得分:1)

根据错误消息,帐户“ domain \ account $”无权访问myTable数据库。

我建议您首先可以使用SQL Server管理系统的安全性来检查他的帐户“ domain \ account $”没有访问(本地)服务器的myTable数据库的权限。

更多详细信息,您可以参考此answer


更新:

据我所知,是否要使用Windows Integrated Security访问SQL Server。首先,应确保IIS服务器和sql服务器位于同一intranet内,并使用同一域。

然后,我建议您确保设置了正确的应用程序池标识帐户,该帐户具有访问sql数据库的权限。

1。您应该在sql server中使用域帐户“ domain \ account”,以确保其具有读写权限

2。在IIS上,打开“应用程序池”设置。对于“身份”选项,选择“自定义帐户”。输入您的用户名域\帐户)和密码。

那么它将很好地工作。

答案 1 :(得分:0)

请检查iispool用户是否具有登录数据库的权限。您可以使用Microsoft Sql Server Management Studio执行此操作。 我通常会忘记它!希望它能对您有所帮助!问候。