2

这是场景:

我在 Active Directory 上创建了一个名为Test的帐户。

此帐户有权读取数据库实例。

我可以通过带有 Windows 身份验证的 SQL Server Visual Management Studio 访问域内的数据。

现在的问题:

我如何在外部> 域将使用 .NET 项目测试访问这些数据?

我把它放在我的 app.config 中:

<connectionStrings>
   <add name="CRM" connectionString="Data Source=server; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
   <identity impersonate="true" userName="domain\user" password="pass"/>
</system.web>

但我仍然收到此错误:

用户“x”登录失败。用户未与受信任的 SQL Server 连接关联。

最后同样重要的是,是的,我确实启用了 SQL 和 Windows 身份验证模式。

4

2 回答 2

2

If SQL server is outside the domain then you have to provide the IP and port of server like this

Change in connectionstring

From

<add name="CRM" connectionString="Data Source=server; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/>

To

<add name="CRM" connectionString="Data Source=212.22.231.11,1433; Initial Catalog=catalog; Integrated Security=SSPI; providerName="System.Data.SqlClient"/>

In the above statement 212.22.231.11 server which has Database hosted in SQL Server. and 1433 is port exposed by SQL Server

于 2012-05-04T17:49:44.643 回答
1

当我在我的 AD 域之外时,我使用以下代码段:

using System.DirectoryServices;
using System.Diagnostics;
using System.Management;
using System.DirectoryServices.AccountManagement;

public bool IsAuthenticated(String domain, String username, String pwd)
{
    // this is a query of the students credentials
    try
    {
        //Bind to the native AdsObject to force authentication.                 
        String domainAndUsername = domain + "\\" + username;
        DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
        Object obj = entry.NativeObject;
        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = "(SAMAccountName=" + username + ")";
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();
        if (null == result)
        {
            return false;
        }
        //Update the new path to the user in the directory.
        _path = result.Path;
        _filterAttribute = (String)result.Properties["cn"][0];
    }
    catch (Exception ex){}
    return true;
}

然后我像这样使用它:

var adAuth = new LdapAuthentication(@"LDAP://snip.edu");            
bool auth = adAuth.IsAuthenticated("snip", "username","password"
if (auth)
{
    // do something}
}
于 2012-05-04T17:57:26.887 回答