windows service sql connection problems

2020-07-13 10:12发布

问题:

I need your help !!!!

I want to connect to sql server from a windows service, but it throw following exception :

Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

My connection string is declared as follow:

<add name="CoreConnectionString" 
     connectionString="Data Source=10.10.2.102;Initial Catalog=DataBaseName;
                       Integrated Security=True" 
     providerName="System.Data.SqlClient" />

When I use user name and password instead of Integrated Security=True It works but in final deployment I cannot use user name and password.

What is wrong what can I do????

回答1:

When you define Integrated Security=True in your connection string, whatever user is currently logged in will try to connect to your database. When running this as a console or Winforms app, this is your own user account.

However, if you run it as a Windows NT Service, it's the service account that this service is running under - in your case obviuosly NT AUTHORITY\ANONYMOUS LOGON.

And the error says it clearly: this user account does not have the permission to connect to the SQL Server.

You have several options:

  • stop your NT service and change the service account to be someone who does have access to the SQL Server

  • allow the NT AUTHORITY\ANONYMOUS LOGON to log into your SQL Server and use your database

  • create a specific user (e.g. an "application user") in your SQL Server and change your connection string to use that user specifically:

    connectionString="Data Source=10.10.2.102;Initial Catalog=DataBaseName;
                      user id=Your-Application-User-here;password=The-Secret-Password"