Exception when attempting to create database after impersonation

0

In my application I have a user input about windows credentials. With these credentials, I impersonate the user in order to create a db with entity framework code first.

  if (isSqlAuthentication)
        {
            using (var context = new RetDbContext.RetContext(data.RetDbConnectionString))
            {
                context.Database.CreateIfNotExists();
            }

        }
        else //impersonate user
        {
            Impersonation.RunAsUser(credentials, LogonType.Network, () =>
            {
                var id = WindowsIdentity.GetCurrent();
                var context = new RetDbContext.RetContext(data.RetDbConnectionString);
                context.Database.CreateIfNotExists();
                context.Dispose();
            });


        }

With sql authentication everything works ok. But if i am trying with windows authentication I got the exception "The provider did not return a ProviderManifest instance." with Inner exception "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))".

The event viewer shows "Login failed for user MYUSER. Reason: Failed to open the explicitly specified database DBNAME. [CLIENT: ]"

I tried to just create the database as impersonated user with this

        Impersonation.RunAsUser(credentials, LogonType.Network, () =>
        {
            string queryString = "CREATE DATABASE testdb;";
            string connectionString = data.RetDbConnectionString;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                command.ExecuteNonQuery();
            }
        });

and also works fine so its not a rights error. My connection string is Data Source=localhost;Initial Catalog=DBNAME;Integrated Security=True;

Could you please help me?

c#
sql
entity-framework
impersonation
asked on Stack Overflow Aug 29, 2018 by Digital_Jo4

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0