C# 使用sql身份验证的sql连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12265047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:41:03  来源:igfitidea点击:

sql connection string using sql authentication

c#sql-serversqlconnection

提问by Neeru

I am using this sql connection string :

我正在使用这个 sql 连接字符串:

string connection = "data source=OSBORNECHARLES2;initial catalog=TWO;
integrated security=False;User ID=userid;Password=PWD";

I am getting this error :

我收到此错误:

A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)

已成功与服务器建立连接,但随后在登录过程中出现错误。(提供程序:共享内存提供程序,错误:0 - 管道的另一端没有进程。)

If I set integrated security=True;it is working.
If I log in with another window user I'm getting error.

如果我设置integrated security=True;它正在工作。
如果我使用另一个窗口用户登录,则会出现错误。

Can you please tell me why I'm getting this error.

你能告诉我为什么我会收到这个错误。

回答by Brian Rudolph

I am going to venture a guess here. I would assume that your instance of SQL is set to only allow Windows\Integrated logins. The userid\password you are setting in the connection string is for SQL logins only, you can't pass another windows user that way. It would appear that you are attempting to do impersonation using the connection string. I wish it was that simple.

我要在这里冒险猜测一下。我假设您的 SQL 实例设置为仅允许 Windows\Integrated 登录。您在连接字符串中设置的用户 ID\密码仅用于 SQL 登录,您不能以这种方式传递另一个 Windows 用户。您似乎正在尝试使用连接字符串进行模拟。我希望就这么简单。

So you likely either need to enable mixed mode security on your sql instance and create sql logins for this user, or you need to impersonate that windows user in your application and then use integrated security.

因此,您可能需要在 sql 实例上启用混合模式安全性并为此用户创建 sql 登录名,或者您需要在应用程序中模拟该 Windows 用户,然后使用集成安全性。

回答by Hassanuzzaman

I think you can you use this

我想你可以用这个

Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;Password=myPassword;

回答by Jeff Codes

I know this is an old question, but what provider were you using. According to thispost, 'SSPI' works with both, SQLClient and Oledb, whereas 'true'/'false' only works with SQLClient.

我知道这是一个老问题,但是您使用的是哪个提供商。根据这篇文章,“SSPI”适用于 SQLClient 和 Oledb,而“true”/“false”仅适用于 SQLClient。

Here'sanother helpful post on this subject, which explains that using 'SSPI' or 'true' will result in it trying to use the windows credentials of the current user.

这是关于此主题另一篇有用的帖子,它解释了使用“SSPI”或“true”将导致它尝试使用当前用户的 Windows 凭据。

I agree with some of the other comments. Likely causes are: 1) SQL Authentication is not enabled on the server. Can do the following to troubleshoot: a. Try accessing using SQL auth connecting through SSMS. If it succeeds than you know SQL Authentication is enabled. 2) The user you're trying to login with that's failing doesn't have permission. a. Make sure the user has a server principal. As an extra measure (though I don't think it matters) ensure that server principal is tied to a database principal on their default database.

我同意其他一些意见。可能的原因是:1) 服务器上未启用 SQL 身份验证。可以执行以下操作来排除故障: a. 尝试使用通过 SSMS 连接的 SQL 身份验证进行访问。如果成功,您就知道启用了 SQL 身份验证。2) 您尝试登录失败的用户没有权限。一种。确保用户具有服务器主体。作为一项额外措施(尽管我认为这并不重要)确保服务器主体与其默认数据库上的数据库主体相关联。

If it's a remote server you would need to enable all the necessary services, which can be done in SQL Server Configuration Manager.

如果是远程服务器,则需要启用所有必要的服务,这可以在 SQL Server 配置管理器中完成。