2

I did a little Googling and I came accross this promising code

System.DirectoryServices.AccountManagement.PrincipalContext pc = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain, "YOURDOMAIN")
// validate the credentials 
bool validatedOnDomain = pc.ValidateCredentials(userName, tb.Text.ToString());

userName is initialized as the Windows login name. It's also a string tb.Text.ToString() is the textbox that is being used for typing the password

Updated code and it's working. Thanks all

MSDN says that PrincipalContext can use two arguments

Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177
  • Hmm... did you add a reference to the `System.DirectoryServices.AccountManagement` assembly, and a suitable `using System.DirectoryServices.AccountManagement;` clause at the top of your source code file? The `PrincipalContext` class shouldn't be coming from your `TestAD` namespace, really ..... – marc_s Feb 15 '12 at 15:27
  • I forgot to add the assemblies. Thanks! That cleared the 2nd error but produced an unusual error. – Cocoa Dev Feb 15 '12 at 15:34
  • That Error #1 still seems to indicate that you have a `PrincipalContext` class in your own `TestAD` namespace - that one might not have the same constructors as the "real" one from `System.DirectoryServices.AccountManagement` .... – marc_s Feb 15 '12 at 15:41

2 Answers2

5

Try to figure out this code.. This is working perfectly in my project.

public bool  ValidateUser(string varDomain, string varUserName, string varPwd)
    {
        Boolean isValidUser;
        using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, varDomain))
        {
            isValidUser = pc.ValidateCredentials(varUserName, varPwd);
        }
        return isValidUser;

    }
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
3

type used in a using statement must be implicitly convertible to 'System.IDisposable'

Means that you need to change your code to:

PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

// validate the credentials 
bool validatedOnDomain = pc.ValidateCredentials(userName, tb.Text.ToString());

Basically it's just telling you that you cannot use a PrincipalContext in a using statement, because PrincipalContext does not implement the interface called IDisposable.

EDIT As marc_s has pointed out below, the PrincipalContext you are using is not the right one. It seems to live in your own namespace. You should be using that from System.DirectoryServices.AccountManagement.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
  • 1
    [Actually - it does implement `IDisposable`](http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principalcontext.aspx) - I'm thinking he's recreated the `PrincipalContext` from the S.DS.AM namespace in his own code (`TestAD.PrincipalContext.....`) – marc_s Feb 15 '12 at 15:29
  • how do you do that? I have the Using statements on top to import the classes I need. – Cocoa Dev Feb 15 '12 at 15:38