UserPrincipal.FindByIdentity throws exception - There is no such object on the server

扶醉桌前 提交于 2019-12-21 05:05:41

问题


I'm struggling with a simple scenario: I would like to retrieve my account from Active Directory using the username and password which I use to log into my computer.

My first issue was that I was receiving a referral from the server when attempting to call UserPrincipal.FindByIdentity. I thought that this was a bit weird, given the fact that PrincipalContext.ValidateCredentials was working fine, but it turns out that my DC path was incorrect.

I wasn't sure how to properly craft my OU/DC string. As such, I found this SO post which helpful provided the following bit of code:

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;
}

After using this code to properly generate my DC string, my error message changed. Now, I am receiving the error "There is no such object on the server." I suspect the issue is either with my OU or how I am calling FindByIdentity.

Here is the location of my user account which I am trying to retrieve:

And here is how I am attempting to access said user:

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);
    }
}

I have also tried calling:

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

these were equally unsuccessful.


回答1:


This Code should work for you Sean I work on AD for BOA currently and use this many times..

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;
}

from MSDN what each parameter is see the list below Parameters

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 Method()




回答2:


I belive the object that does not exist is:

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

Users is a container, not an OU. So correcty you need:

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



来源:https://stackoverflow.com/questions/14162099/userprincipal-findbyidentity-throws-exception-there-is-no-such-object-on-the-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!