Check UserID exists in Active Directory
Check UserID exists in Active Directory
Hi every one today we are going to see whether a user exist in the active directory or not using C#.
- Please add the
System.DirectoryServices.AccountManagement
namespace and assembly to your code.
[csharp]
public string DoesUserExist(string userName)
{
using (var domainContext = new PrincipalContext(ContextType.Domain, "spl"))
{
using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
{
if (foundUser != null)
return foundUser.Name;
else
return "User Does not exist ";
}
}
}
[/csharp]