1

I ran a web app on IIS and I got a

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

This is a exception when the function DbMigrator.Update() is called

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();
}

And here is the stack trace:

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 

Here is my connection string:

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

The code works fine in other machine, but in my computer it doesn't work and I can't find out the root cause.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Allen S
  • 53
  • 1
  • 7
  • If integrated security is set to true, it will refer to the application pool credential. Check that the credential has access to the database. – j.f. Apr 23 '19 at 08:29
  • In [this question you just deleted](https://stackoverflow.com/q/57455229/3404097) were you trying to ask what constraint to declare that would enforce that every child element in Family children is a Human id? [Are multiple foreign keys in a single field possible?](https://stackoverflow.com/q/7094170/3404097) [Is storing a delimited list in a database column really that bad?](https://stackoverflow.com/q/3653462/3404097) [Trying to make a PostgreSQL field with a list of foreign keys in Django](https://stackoverflow.com/a/35958727/3404097) – philipxy Aug 12 '19 at 08:29

2 Answers2

2

According to the error message, the account 'domain\account$' does not have access to the myTable database.

I suggest you could firstly use the SQL server management system's security to check he account 'domain\account$' does not have access to the (local) server's myTable database.

More details, you could refer to this answer.


Update:

As far as I know, if you want to access SQL Server Using Windows Integrated Security. You should firstly make sure your IIS server and the sql server is inside the same intranet and use the same domain.

Then I suggest you could make sure you have set the right application pool identity account which has the permission to access the sql database.

1.You should the domain account "domain\account" in sql server to make sure it has the read and write permission

2.On IIS, open Application Pool settings.For Identity option, choose "Custom Account". Enter your username domain\account) and password.

Then it will work well.

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • Sorry for replying so late, I didn't found the user name in my sqlserver, so I am wondering how the login user came up. as it is not in sqlserver. – Allen S Apr 24 '19 at 06:54
  • Thanks very much!!! this did solve my problem. I re-bind the ApplicationPool for the website in iis. – Allen S Apr 28 '19 at 09:37
1

Please, check the iispool user has permissions to log in to database. You can do this using the Microsoft Sql Server Management Studio. I usually forget it! I Hope its helps you! Greetings.

Sebas
  • 11
  • 4
  • Hi @Sebas, Do you mean the IIS APPPOOL\AppPoolOfApp in the Sql server? I changed the permission but still not work. Thank you all the same. – Allen S Apr 24 '19 at 09:07