SQL 登录失败。登录名来自不受信任的域,不能用于 Windows 身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14170927/
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
Login failed. The login is from an untrusted domain and cannot be used with Windows authentication
提问by Abhi
My webpages are on secured server (https), and I am trying to connect the SQL Server 2008 Database, which is normal server. I am writing connectionstring on page itself, not in web.config file. And I am getting following error:-
我的网页在安全服务器 (https) 上,我正在尝试连接 SQL Server 2008 数据库,这是普通服务器。我在页面本身上编写连接字符串,而不是在 web.config 文件中。我收到以下错误:-
System.Data.SqlClient.SqlException: Login failed.
The login is from an untrusted domain and cannot be used with Windows authentication.
Please help, how can I connect it, does I have to make some webservices for it.
请帮助,我该如何连接它,我是否必须为它制作一些网络服务。
my code is as below:
我的代码如下:
public void FillCity()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "integrated security=SSPI;data source=dev-fcb; user id=sa;password=password;"
+"persist security info=False;database=mediapro";
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from StateCityMaster where IsActive='1' order by CityName", con);
DataSet ds = new DataSet();
da.Fill(ds);
string CityName = string.Empty;
if (ds.Tables[0].Rows.Count > 0)
{
CityName = ds.Tables[0].Rows[0]["CityName"].ToString();
}
DataSet dset = new DataSet();
da.Fill(dset);
if (dset.Tables[0].Rows.Count > 0)
{
drpCity.DataSource = dset;
drpCity.DataTextField = "CityName";
drpCity.DataValueField = "CityName";
drpCity.DataBind();
}
drpCity.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
回答by
Your connection string is telling it to use integrated security SSPI, which will use the Windows credentials.
您的连接字符串告诉它使用集成安全 SSPI,它将使用 Windows 凭据。
Set Integrated Security
to false
if you are going to be providing the username and password.
设置Integrated Security
到false
如果你将要提供的用户名和密码。
Also, consider putting your connection string inside of the web.config file - it is more secure and reusable.
此外,请考虑将您的连接字符串放在 web.config 文件中 - 它更安全且可重用。
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
如果为 false,则在连接中指定用户 ID 和密码。如果为 true,则使用当前的 Windows 帐户凭据进行身份验证。识别的值为true、false、yes、no和sspi(强烈推荐),相当于true。如果指定了用户 ID 和密码并且集成安全设置为 true,则用户 ID 和密码将被忽略并使用集成安全。
回答by CodeCaptain
I created a new Asp.Net Core MVC site and I had this same error. I was attempting to connect to my company's database while connected to the network via VPN. Here's what worked for me:
我创建了一个新的 Asp.Net Core MVC 站点,但遇到了同样的错误。我试图在通过 VPN 连接到网络的同时连接到我公司的数据库。以下是对我有用的内容:
Instead of
代替
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=SSPI;Trusted_Connection=True;"
I used
我用了
"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=False;Trusted_Connection=False;"
The key change was to make sure that Trusted_Connection=False, which is consistent with the error message. Generally I would not use an untrusted connection. In this case I was connecting to a dev/test database so it's fine.
关键的更改是确保 Trusted_Connection=False,这与错误消息一致。通常我不会使用不受信任的连接。在这种情况下,我连接到开发/测试数据库,所以没问题。
回答by Thierry_S
Old question, and my symptoms are slightly different, but same error. My connection string was correct (Integrated security, and I don't provide user and pwd) with data source
set to 127.0.0.1
. It worked fine for years.
老问题,和我的症状略有不同,但同样的错误。我的连接字符串是正确的(集成安全性,我不提供用户和密码)data source
设置为127.0.0.1
. 多年来它运行良好。
But recently I added a line in the static host file for testing purposes (C:\Windows\System32\drivers\etc\hosts
)
但是最近我在静态主机文件中添加了一行用于测试目的 ( C:\Windows\System32\drivers\etc\hosts
)
127.0.0.1 www.blablatestsite.com
Removing this line and the error is gone.
删除这一行,错误就消失了。
I got a clue from this article (https://support.microsoft.com/en-gb/kb/896861) which talks about hostnames and loopback.
我从这篇关于主机名和环回的文章 ( https://support.microsoft.com/en-gb/kb/896861) 中得到了一个线索。
Other possible fix (if you need to keep that line in the hosts file) is to use the hostname (like MYSERVER01
) instead of 127.0.0.1
in the data source
of the connection string.
其他可能的解决方法(如果您需要在主机文件中保留该行)是使用主机名(如MYSERVER01
)而不是127.0.0.1
在data source
连接字符串的 中。
回答by Aage
For future googlers:
对于未来的谷歌员工:
If you doneed Integrated Security
and are getting this error it might be you're using a local account
instead of a domain account
.
如果您确实需要Integrated Security
并收到此错误,则可能是您使用的是 alocal account
而不是domain account
。
I came across this running Visual Studio locally and trying to connect to a database on another machine. A workaround was to run Visual Studio as a different user, the prompt didn't work but running the command below did (make sure to replace DOMAIN\USER
and you will be asked to provide credentials):
我在本地运行 Visual Studio 并尝试连接到另一台机器上的数据库。一种解决方法是以不同的用户身份运行 Visual Studio,提示不起作用,但运行下面的命令起作用(确保替换DOMAIN\USER
并且系统会要求您提供凭据):
runas /netonly /user:DOMAIN\USER "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"
runas /netonly /user:DOMAIN\USER "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"