28

How can we check whether the USERID exists in Active Directory or not.

I have LDAP String and UserID, can I find whether that UserID exists in Active Directory or not. I am using this for ASP.NET Web Application (.NET 3.5)

msbyuva
  • 3,467
  • 13
  • 63
  • 87

2 Answers2

51

You can do something along the lines of (replacing domain with the domain you're authenticating against or removing the parameter altogether):

public bool DoesUserExist(string userName)
{
    using (var domainContext = new PrincipalContext(ContextType.Domain, "DOMAIN"))
    {
        using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
        {
            return foundUser != null;
        }
    }
}

To achieve checking for if a user exists. This comes from the System.DirectoryServices.AccountManagement namespace and assembly.

You can find more information at http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx

You may want to check more into PrincipalContext as it has interesting methods for authenticating user credentials and such.

Joshua Rodgers
  • 5,317
  • 2
  • 31
  • 29
  • 3
    +1 What I would do, only better explained. Also, you can omit the doamin name to use the local domain - handy if you've only got the one domain and don't need to know its name. – Grhm Dec 15 '10 at 20:55
  • Thanks it helped.. the new namespace System.DirectoryServices.AccountManagement helped..!! Well I am little bit confised at second Parameter "DOMAIN" ..? do we need to give it or not if we give is it Active Directory's Domain Name ?... – msbyuva Dec 15 '10 at 21:07
  • You can delete that parameter if you want to connect to the system's default domain. Otherwise you'll need to specify the domain you're wanting to connect to. – Joshua Rodgers Dec 15 '10 at 21:15
  • Don't forget to accept answers if they're what you're looking for. :) – Joshua Rodgers Dec 15 '10 at 21:40
9

I would use the System.DirectoryServices.AccountManagement namespace.

string UserID = "grhm";
bool userExists = false;

using (var ctx = new PrincipalContext(ContextType.Domain))
{
    using (var user = UserPrincipal.FindByIdentity(ctx, UserID))
    {
        if (user != null)
        {
            userExists = true;
            user.Dispose();
        }
    }
}

See http://msdn.microsoft.com/en-us/library/bb344891.aspx for more info

Grhm
  • 6,726
  • 4
  • 40
  • 64
  • Thank You it worked, yeah the new namespace. System.DirectoryServices.AccountManagment made very easy... – msbyuva Dec 15 '10 at 21:05
  • I see in some examples there is another parameter "DOMAIN" for the PrincipleContext... Do we need to use it or not ?.. – msbyuva Dec 15 '10 at 21:07
  • You can use other parameters to specify the domain you want to contect to - as in the other answer. I see Joshua has explained this already above... – Grhm Dec 15 '10 at 22:19
  • You declare `user`, but I never see `usr` declared. Should those be the same variable? – Brian J Oct 23 '13 at 20:49
  • @BrianJ: Yep, they should be. Edited to fix it. – Grhm Oct 24 '13 at 07:51
  • You don't need to dispose(), as the Using() does that for you. – Fandango68 Jan 25 '19 at 05:44
  • Also this wouldn't work if it ran from IIS, as IIS_USER or what ever account is running from the web service will need to be added into AD as well and given high priveleges to run AD methods. – Fandango68 Jan 25 '19 at 06:02