Active Directory Authentication, Part 2
After getting some advise about the previous piece of code, I decided to dig deeper, and here is what I have now:
        public bool IsValidLogin(string username, string password)        
        {        
               ActiveDirectoryMembershipProvider membershipProvider = new ActiveDirectoryMembershipProvider();        
               NameValueCollection args = new NameValueCollection();        
               args["connectionStringName"] = connectionStringName;        
               membershipProvider.Initialize("Extranet", args);        
               return membershipProvider.ValidateUser(username, password);        
}
I would probably want to cache the provider, but this is the easiest way I could find to do it, and if something isn't working, I can blame Microsoft :-). I don't think that I need to worry about thread-safety issues, because providers must be thread-safe anyway.
 

Comments
If you register the ActiveDirectoryMembershipProvider in your web.config file, you can just write:
return Membership.ValidateUser(username, password);
This will do all of the configuration and provider caching logic for you.
Hope this helps,
Scott
Scott,
Thanks for the suggestion, I don't think that this is possible to do because I need this piece of code to run outside of a web context as well (tests, etc), which may be a problem.
hmm. I am surely not an expert with the membership provider, but can you not config it in the app.config for the tests?
Comment preview