Developping for the translation industry RSS 2.0



 Wednesday, 10 October 2007

It’s a shame how many applications, even popular ones, have huge security gaps regarding passwords.  The most recent case to hit the news was Reddit but this is only the tip of the iceberg.  How many internals applications out there uses plain text password.  A whole lot!  You probably did it at some point, and so do I.  But the object of this post is not to rant and whine about that situation.  I just want to show those who don’t know about it how easy it is to integrate Windows Active Directory authentication to your application.  So now, if you read this my blog, no more excuses to have that “password” field in your database.

First you need to add a reference in your project to System.DirectoryServices

Here is the code, I will catch you up on the other side.

try

String Username = "username";

String domainAndUsername = "domain\\username";

String Password = "password";

 

DirectoryEntry entry = new DirectoryEntry("", domainAndUsername, Password);

DirectorySearcher search = new DirectorySearcher(entry);

 

search.Filter = "(SAMAccountName=" + Username + ")";

 

// search.FindOne() will throw an exception if there is a bad username/password combination provided

SearchResult result = search.FindOne();

 

// User is authenticated

catch 

    throw new System.Security.SecurityException("Access denied.");

End Try

Pretty straightforward isn’t it?  Obviously, you need to change the first lines with the user’s input but other than that, it’s all that’s needed for a basic username/password authentication using Active Directory.  You can copy this code and use it in your application and see for yourself.  If you want more information on what you can get from Active Directory, there is a good article on the different name attributes here.  You can also go to the homepage of System.DirectoryService on MSDN here.

I want to add that I’m not an Active Directory expert.  If a reader see something wrong with this code, please let me know and I’ll update it right away!

Other Posts:

How to create user accounts in active directory using C#

Wednesday, 10 October 2007 10:59:42 (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
C# | Code Snippet

Navigation
Advertisement
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Stanislas Biron
Sign In
Statistics
Total Posts: 135
This Year: 0
This Month: 0
This Week: 0
Comments: 1
All Content © 2017, Stanislas Biron