0

I am using following code to validate AD users

string strLDAP = "LDAP://dc=ADServerIP/cn=Users,DC=Domain;
DirectoryEntry entry = new DirectoryEntry(strLDAP, usr, pwd);
object nativeObject = entry.NativeObject;
return true;

I am getting the following exception when executing

object nativeObject = entry.NativeObject;

System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_NativeObject()

The same code is working for another AD server. What may be the issue?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vml19
  • 3,816
  • 11
  • 45
  • 63
  • http://stackoverflow.com/questions/1063642/how-to-know-if-my-directoryentry-is-really-connected-to-my-ldap-directory It might be a simple connection issue. The network is invoved at NativeObject, not at New() –  Jun 04 '12 at 03:10

1 Answers1

7

Are you working on .NET 3.5 or newer? If so, you can use the System.DirectoryServices.AccountManagement namespace and easily verify your credentials:

// create a "principal context" - e.g. your domain (could be machine, too)
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", usr, pwd))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

It's simple, it's reliable, it's 100% C# managed code on your end - what more can you ask for? :-)

Read all about it here:

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • where to specify AD server name And My Domain? – vml19 Jun 04 '12 at 11:00
  • @Roshe: you need to provide the domain name as second parameter to the constructor of `PrincipalContext` - no AD server name needed. – marc_s Jun 04 '12 at 11:42
  • Thanks Marc_s, what about the comment on guest user by chrish in this http://stackoverflow.com/questions/290548/c-sharp-validate-a-username-and-password-against-active-directory do I need to configure anything for guest user if it is enabled? – vml19 Jun 04 '12 at 15:43