windows服务sql连接问题

2021-09-27 00:00:00 sql-server windows-services

我需要你的帮助!!!!!!

I need your help !!!!

我想从 Windows 服务连接到 sql server,但它抛出以下异常:

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

用户NT"登录失败权威\匿名登录'.

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" />

当我使用用户名和密码而不是 Integrated Security=True 时它可以工作,但在最终部署中我不能使用用户名和密码.

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????

推荐答案

当您在连接字符串中定义 Integrated Security=True 时,当前登录的任何用户都将尝试连接到您的数据库.将此作为控制台或 Winforms 应用运行时,这是您自己的用户帐户.

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.

但是,如果您将它作为 Windows NT 服务运行,则该服务是在该服务帐户下运行的 - 在您的情况下显然 NT AUTHORITY\ANONYMOUS LOGON.

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.

并且错误明确指出:此用户帐户没有连接到 SQL Server 的权限.

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

您有多种选择:

  • 停止您的 NT 服务并将服务帐户更改为有权访问 SQL Server 的人

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

允许 NT AUTHORITY\ANONYMOUS LOGON 登录到您的 SQL Server 并使用您的数据库

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

在您的 SQL Server 中创建一个特定用户(例如应用程序用户")并更改您的连接字符串以专门使用该用户:

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"

相关文章