NerdyHearn
Home

IPhone SMS To Gmail

Consulting
Security Alerts

Blog
Software
Projects
Links
About
Contact

Blog
FriendFeed
Facebook
Flickr
Twitter
Toluu
Last.fm
Picasa
LinkedIn

NerdyHearn - Blog


<< Back To All Blogs

Validate a Windows Username and Password against Active Directory

Friday, January 8th, 2010

I came across a need to validate a windows username and password explicitly against a domain server for the purpose of verification. I have had to implement just about every other scenario with regards to authentication in the past already, but normally because the user is already authenticated, .NET will handle Domain negotiation on it's own so you don't have to get into this, until you reach the scenario that you want to verify the user's credentials in whatever type of application.

This turned out to be much easier then I expected, as handling NTLM domain authentication in HTTP requests can be quite the task at times, but to my surprise (and delight), you simply need the following:

PrincipalContext myContext = new PrincipalContext(ContextType.Domain, "MyDomain");
bool validUser = myContext.ValidateCredentials("MyDomainMyUsername", "MyPassword");

validUser will return true if you are successfully authenticated.

A few other notes:
1. These are used from the System.DirectoryServices.AccountManagement namespace
2. The assembly for this namespace is not included by default in most scenarios, so you'll need to add a reference to the System.DirectoryServices.AccountManagement.dll assembly
3. Even though you are specifying the domain in your PricipalContext, it is important to note that this won't work unless you include your full domain and username path for the username argument in ValidateCredentials. For example, nerdyhearn would not work for the username validation, whereas nerdydomainnerdyhearn would.

Authenticatin' Tom Out.

Tags

CSharp ASP.NET Howto

Related Blogs

Reading a Microsoft Project file (mpp) in C#
Retrieving the SID of a user or group account using the Win32 SDK and C#
Reading Digital Signatures from InfoPath Forms in MOSS 2007 and WSS 3.0 Workflow
Programatically Retrieving an Assembly's PublicKeyToken through a PowerShell CmdLet
Retrieving data from SharePoint SOAP Requests using LINQ

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:


Please enter the text from the image:



NerdyHearn - Latest tech news relating to C#, SharePoint, PHP, general development, and more. LiveNation Feed - Feeds of all your local artists, concerts, and events through your favorite RSS reader SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers Tweets2Mail - Backup and search your twitter stream from Gmail TimeSinceI - Track how long you have quit smoking, how long you have been married, or how long you have done anything.