登录失败.登录来自不受信任的域,不能与Windows身份验证一起使用

Abh*_*bhi 11 sql https web-services sql-server-2008

我的网页在安全服务器(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.
Run Code Online (Sandbox Code Playgroud)

请帮助,如何连接它,我是否必须为它做一些web服务.

我的代码如下:

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();
}
Run Code Online (Sandbox Code Playgroud)

小智 30

您的连接字符串告诉它使用集成安全性SSPI,它将使用Windows凭据.

设置Integrated Securityfalse如果你将要提供的用户名和密码.

另外,请考虑将您的连接字符串放在web.config文件中 - 它更安全且可重用.

来自http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=VS.100).aspx:

如果为false,则在连接中指定用户ID和密码.如果为true,则使用当前Windows帐户凭据进行身份验证.识别的值是true,false,yes,no和sspi(强烈推荐),这相当于true.如果指定了用户ID和密码并且Integrated Security设置为true,则将忽略用户ID和密码,并将使用Integrated Security.


Cod*_*ain 13

我创建了一个新的 Asp.Net Core MVC 站点,但遇到了同样的错误。我试图在通过 VPN 连接到网络时连接到公司的数据库。这对我有用:

代替

"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=SSPI;Trusted_Connection=True;"
Run Code Online (Sandbox Code Playgroud)

我用了

"DevConnection": "Server=DevDBServer;Database=MyDatabase;User ID=some_userid;Password=some_password;Integrated Security=False;Trusted_Connection=False;"
Run Code Online (Sandbox Code Playgroud)

关键的更改是确保 Trusted_Connection=False,这与错误消息一致。一般来说,我不会使用不受信任的连接。在本例中,我连接到开发/测试数据库,所以没问题。