0

I'm trying to install database to LocalDB from installer class. When trying to open SQLConnection, I get an error:

Login failed for user 'NT AUTHORITY\SYSTEM'

It seems that the installer class runs from the system account, and LocalDB cannot be used by it.

Is there a way to connect LocalDB from installer?

Maybe it's possible to run installer class (or part of the code inside it) from user account (but administrative privileges are still required for the rest of installer's actions)?

The installer can be used on different machines, so I cannot know user's credentials beforehand and cannot make any changes in the system manually, only from installer class code (C#).

Aleksey Shubin
  • 1,960
  • 2
  • 20
  • 39

1 Answers1

0

I have not solved this problem and gived up on LocalDB. However some time later I had a similar problem - connect to LocalDB from a Windows service. Windows service runs from the same NT AUTHORITY\SYSTEM account, so the same solution may help in this case.

The solution is to use LocalDB shared instances. Instances are managed via SqlLocalDB utility.

  1. Create a new LocalDB instance:

    SqlLocalDB.exe create "InstanceName"
    
  2. Share the instance:

    SqlLocalDB.exe share "InstanceName" "SharedInstanceName"
    
  3. Start the instance:

    SqlLocalDB.exe start "InstanceName"
    
  4. Shared instances are named .\SharedInstanceName, so in your connection string it will look like (localdb)\.\SharedInstanceName. You can find your instance name using:

    SqlLocalDB.exe info
    
  5. Connect to the instance using sqlcmd utility, and add NT AUTHORITY\SYSTEM user to SQL users and set appropriate permissions for it:

    sqlcmd.exe -S (localdb)\.\SharedInstanceName -i "<path-to-SQL-file>"
    

    SQL file contents:

    CREATE LOGIN [NT AUTHORITY\SYSTEM] FROM WINDOWS WITH DEFAULT_DATABASE=[master]
    GO
    ALTER SERVER ROLE [sysadmin] ADD MEMBER [NT AUTHORITY\SYSTEM]
    GO
    


    An assumption (I have not checked it): for some reason SQL installer class seems to be running from NT AUTHORITY\SYSTEM account, so if you try to do the steps above from the installer class, it most likely will fail on step 5, when trying to connect to the instance. So this steps need to be done some other way, for example from a .bat file which is run directly from the installer before the installer class is run.
Aleksey Shubin
  • 1,960
  • 2
  • 20
  • 39