5

単純なシナリオで苦労しています。コンピュータへのログインに使用するユーザー名とパスワードを使用して、Active Directory からアカウントを取得したいと考えています。

最初の問題は、UserPrincipal.FindByIdentity を呼び出そうとしたときに、サーバーから紹介を受けていたことです。PrincipalContext.ValidateCredentials が正常に機能していたという事実を考えると、これは少し奇妙だと思いましたが、DC パスが正しくないことがわかりました。

OU/DC 文字列を適切に作成する方法がわかりませんでした。 そのため、次のコードを提供するのに役立つこのSO投稿を見つけました。

private static string GetDomainControllerString()
{
    string pdc;
    using (var context = new PrincipalContext(ContextType.Domain))
    {
        string server = context.ConnectedServer; // "pdc.examle.com"
        string[] splitted = server.Split('.'); // { "pdc", "example", "com" }
        IEnumerable<string> formatted = splitted.Select(s => String.Format("DC={0}", s));// { "DC=pdc", "DC=example", "DC=com" }
        string joined = String.Join(",", formatted); // "DC=pdc,DC=example,DC=com"

        // or just in one string

        pdc = String.Join(",", context.ConnectedServer.Split('.').Select(s => String.Format("DC={0}", s)));
    }

    return pdc;
}

このコードを使用して DC 文字列を適切に生成した後、エラー メッセージが変わりました。現在、「サーバー上にそのようなオブジェクトはありません」というエラーが表示されます。問題は、私の OU か、FindByIdentity の呼び出し方法にあると思われます。

取得しようとしているユーザー アカウントの場所は次のとおりです。

ここに画像の説明を入力

そして、これが私がそのユーザーにアクセスしようとしている方法です:

private static void Main(string[] args)
{
    const string Domain = "SLO1.Foo.Bar.biz";
    const string DefaultOU = "OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz";
    const string username = @"sanderso";
    const string password = "**********";

    var principalContext = new PrincipalContext(ContextType.Domain, Domain, DefaultOU, ContextOptions.Negotiate, username, password);
    bool areCredentialsValid = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate);

    if (areCredentialsValid)
    {
        UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);
    }
}

私も電話してみました:

UserPrincipal.FindByIdentity(principalContext, IdentityType.Name, "Sean Anderson");
UserPrincipal.FindByIdentity(principalContext, "Sean Anderson");

これらは同様に失敗しました。

4

2 に答える 2

7

存在しないオブジェクトは次のとおりだと思います。

"OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz"

ユーザーはコンテナであり、OU ではありません。したがって、必要な修正:

"CN=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz"

于 2013-01-04T20:37:39.167 に答える
6

このコードはあなたのために働くはずです Sean 私は現在 BOA の AD に取り組んでおり、これを何度も使用しています..

public bool UserExists(string username)
{
   // create your domain context
   PrincipalContext domain = new PrincipalContext(ContextType.Domain);

   // find the user
   UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username);

   return foundUser != null;
}

MSDNから各パラメータが何であるかは、以下のリストを参照してください パラメータ

context
  Type: System.DirectoryServices.AccountManagement.PrincipalContext

  The PrincipalContex that specifies the server or domain against which operations are performed.

identityType
  Type: System.DirectoryServices.AccountManagement.IdentityType

  A IdentityType enumeration value that specifies the format of the identityValue parameter.

identityValue
  Type: System.String

  The identity of the user principal. This parameter can be any format that is contained in the IdentityType enumeration.

Return Value
  Type: System.DirectoryServices.AccountManagement.UserPrincipal
  A UserPrincipal object that matches the specified identity value and type, or null if no matches are found.

UserPrincipal.FindByIdentity メソッド()

于 2013-01-04T19:06:13.643 に答える