2

シナリオは次のとおりです。

Testという名前のActiveDirectoryで作成されたアカウントがあります。

このアカウントには、データベースのインスタンスを読み取る権限があります。

ドメイン内のデータにアクセスするには、Windows認証を使用したSQL Server VisualManagementStudioを使用します。

今問題:

ドメインの外部で.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'のログインに失敗しました。ユーザーは信頼できるSQLServer接続に関連付けられていません。

最後になりましたが、はい、SQLとWindowsの両方の認証モードが有効になっています。

4

2 に答える 2

2

SQLサーバーがドメイン外にある場合は、次のようにサーバーのIPとポートを指定する必要があります

接続文字列の変更

から

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

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

上記のステートメントでは、SQLServerでデータベースがホストされている212.22.231.11サーバー。1433はSQLServerによって公開されたポートです

于 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 に答える