简体   繁体   English

Windows服务SQL Server错误 - “用户登录失败”

[英]Windows Service SQL Server error - “Login failed for user”

I have a Windows Service written in C# being run on a Windows Server 2012 R2 machine which connects to a remote SQL Server 2012 instance also on a Windows Server 2012 R2 Server. 我有一个用C#编写的Windows服务在Windows Server 2012 R2机器上运行,该机器也连接到Windows Server 2012 R2服务器上的远程SQL Server 2012实例。

The SQL Connection String is defined in the App Config as follows: SQL连接字符串在App Config中定义如下:

<connectionStrings>
    <add name="destinationConnectionString" providerName="System.Data.SqlClient"
    connectionString="Server=10.42.42.10;Database=mydb;User ID=myuser;Password=mypass" />
</connectionStrings>

When running the service, the output error log shows: 运行该服务时,输出错误日志显示:

System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'myuser'. System.Data.SqlClient.SqlException(0x80131904):用户'myuser'登录失败。

Error Number:18456, State:1, Class:14 错误号码:18456,州:1,班级:14

So I check the SQL Server Logs to find out more information about the error and find this 所以我检查SQL Server日志以找到有关错误的更多信息并找到它

Login failed for user myuser Reason: Password did not match for the login provided" 用户myuser登录失败原因:密码与提供的登录名不匹配“

Source: Logon Message: Error: 18456, Severity: 14, State: 8. 来源:登录消息:错误:18456,严重性:14,状态:8。

The problem is I know these credentials are correct. 问题是我知道这些凭据是正确的。 I've checked and double checked. 我检查过并仔细检查过。

I am able to prove the credentials work by opening SQL Server Management Studio on the source machine, and connecting to the remote SQL Server using the exact same Server, User ID and Password with "SQL Server Authentication" authentication mode. 我能够通过在源计算机上打开SQL Server Management Studio,并使用完全相同的服务器,用户ID和密码以及“SQL Server身份验证”身份验证模式连接到远程SQL Server来证明凭据有效。

In fact I have 2 different users that I'm able to connect with using SSMS, but that fail using the Windows service. 事实上,我有2个不同的用户,我可以使用SSMS连接,但使用Windows服务失败。 If I am able to connect to the remote machine using mixed mode auth directly in SSMS. 如果我能够在SSMS中直接使用混合模式auth连接到远程计算机。 Why not in my Windows service? 为什么不在我的Windows服务中?

I'm at a loss here, could anyone suggest what the issue might be? 我在这里不知所措,任何人都可以提出问题所在吗?

Edit: I can even successfully connect to the remote machine using the following command: 编辑:我甚至可以使用以下命令成功连接到远程计算机:

sqlcmd -S 10.42.42.10 -U myuser -P mypass -d mydb

The proper syntax for specifying the initial database is: 指定初始数据库的正确语法是:

Server=10.42.42.10; 服务器= 10.42.42.10; Initial Catalog =mydb;User ID=myuser;Password=mypass 初始目录 = mydb;用户ID = myuser;密码= mypass

My best guess is that it is ignoring the "Database" in your string and possibly trying to connect to the default database setting for the login (possibly master maybe?) and doesn't have at least public role to it. 我最好的猜测是它忽略了你的字符串中的“数据库”,并且可能尝试连接到登录的默认数据库设置(可能是主人?)并且至少没有公共角色。 Although I've found out that "Database" is a valid alternative. 虽然我发现“数据库”是一个有效的替代方案。

It's possible there is a character in your password that is tripping it up. 密码中有一个字符可能会绊倒它。 Semicolons and equal signs will be particularly problematic. 分号和等号将特别成问题。 Try putting double quotes around your User ID and Password like: 尝试在您的用户ID和密码周围加上双引号,如:

Server=10.42.42.10;Initial Catalog=mydb;User ID="myuser";Password="mypass" Server = 10.42.42.10;初始目录= mydb;用户ID =“myuser”;密码=“mypass”

If there's a quotation mark in your password you will have to replace it with two of them for each occurrence. 如果您的密码中有引号,则每次出现时都必须将其替换为两个。

Once you get your connection string built, then you have to ensure that it will parse properly in XML. 一旦建立了连接字符串,就必须确保它将在XML中正确解析。 Which means escaping control characters and the other special characters such as: 这意味着转义控制字符和其他特殊字符,例如:

< with &lt;
> with &gt;
& with &amp;
' with &apos;
" with &quot;

If you open up your app config file in a browser it should show you if it looks proper. 如果您在浏览器中打开您的应用配置文件,它应该显示它是否正常。

I recommend adding the contents of your entire connection string to our output log of your service. 我建议将整个连接字符串的内容添加到服务的输出日志中。 That way you can see what it looks like. 这样你就可以看到它的样子。 At the point before you attempt to open the connection, get it with: 在您尝试打开连接之前,请使用以下命令:

string connStr = ConfigurationSettings.AppSettings("myConnectionString");

Then output the connStr value to the log. 然后将connStr值输出到日志。 It will show you what values are being used at least. 它将向您显示至少使用的值。

You might use some special character in password, that is reserved for XML. 您可以在密码中使用一些特殊字符,这是为XML保留的。 If so, you'll need to use escape character as a replacement. 如果是这样,您将需要使用转义字符作为替代。

For instance, if your password is PASS<WORD the valid entry in config file would be: PASS&lt;WORD 例如,如果您的密码是PASS<WORD ,配置文件中的有效条目将是: PASS&lt;WORD

I have used this site to find the correct pattern 我使用这个网站找到了正确的模式

What you have is valid: Server=myServerAddress;Database=myDataBase;User Id=myUsername; 你拥有的是有效的:Server = myServerAddress; Database = myDataBase; User Id = myUsername; Password=myPassword; 密码= MYPASSWORD;

Are you trying to connect to an instanced sql server? 您是否尝试连接到实例化的SQL Server? Server=10.42.42.10/SomeInstance 服务器= 10.42.42.10 / SomeInstance

I suggest adding the semicolon, ; 我建议加分号; , after the password in the connection string. ,在连接字符串中的密码之后。 Perhaps it's being parsed weirdly. 也许它被奇怪地解析了。

I also wonder whether: is a semicolon in your actual password? 我也想知道:你的实际密码是否是分号?

Maybe you're attempting a Windows account but there's also a SQL account named the same way. 也许您正在尝试使用Windows帐户,但也有一个以相同方式命名的SQL帐户。 If so, make sure you prefix the Windows account with the domain name. 如果是这样,请确保在Windows帐户前添加域名前缀。 Eg you might have Windows account of MYDOMAIN\\sa with P@ssword , whilst SQL server account is sa and its password is P@ssw0rd . 例如,您可能拥有带有P@sswordMYDOMAIN\\sa Windows帐户,而SQL服务器帐户是sa ,其密码是P@ssw0rd

To make sure what accounts exists, log in the SQL server as an admin and check what accounts are listed under "Security" node of both the server itself and the database of interest. 要确定存在哪些帐户,请以管理员身份登录SQL Server,并检查服务器本身和感兴趣的数据库的“安全”节点下列出的帐户。

I had the same problem and here is my solution: 我有同样的问题,这是我的解决方案:

I found out that I was using named instance of an SQL server and changed Server of my connection string from Server=localhost to Server=localhost\\SQLEXPRESS and everything worked just fine for me! 我发现我使用SQL服务器的命名实例,改变了Server从我的连接字符串的Server=localhostServer=localhost\\SQLEXPRESS和一切工作只是对我很好! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM