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

倖福魔咒の 提交于 2019-12-03 16:59:56

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

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"

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