UserPrincipal.FindByIdentity抛出异常 - 服务器上没有此类对象

Sea*_*son 5 c# directoryservices active-directory

我正在努力解决一个简单的问题:我想使用用于登录计算机的用户名和密码从Active Directory检索我的帐户.

我的第一个问题是我在尝试调用UserPrincipal.FindByIdentity时从服务器收到推荐.我认为这有点奇怪,因为PrincipalContext.ValidateCredentials工作正常,但事实证明我的DC路径不正确.

我不确定如何正确制作我的OU/DC字符串.因此,我发现这个 有用的帖子提供了以下代码:

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

使用此代码正确生成我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我也试过打电话:

UserPrincipal.FindByIdentity(principalContext, IdentityType.Name, "Sean Anderson");
UserPrincipal.FindByIdentity(principalContext, "Sean Anderson");
Run Code Online (Sandbox Code Playgroud)

这些同样不成功.

Met*_*Man 6

本代码应该适合你Sean我目前在AD工作BOA并多次使用..

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

从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.
Run Code Online (Sandbox Code Playgroud)

UserPrincipal.FindByIdentity方法()


Dar*_*aro 6

我相信不存在的对象是:

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

用户是容器,而不是OU.所以正确你需要:

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