Programmatically Authenticating Users Using ADAM

by Ray 3. July 2010 13:50

Recently I was attempting to migrate an application from Java to .NET that leveraged ADAM as it’s user store. On first look you think, “No problem, .NET comes with an Active Directory Provider, this should be easy.” For me this wasn’t the case, a slew of problems eventually had me looking at a way to do the authentication myself.  After doing some Binging I finally came across a very interesting site that was completly dedicated to programming .NET applications with Active Directory here<

Here is the process that I went through to do this, this is assuming that you have ADAM already installed and configured on a machine somewhere.

.NET Comes with two namespaces that are essential when working with Active Directory, System.DirectoryServices which will be referred to as SDS and System.DirectoryServices.Protocols which will be referred to as SDS.P.

In order to streamline the process I created an Authentication class that I could call throughout my application:

 /// <summary> 
     /// Contains methods used to authenticate users 
     /// </summary> 
     public  class  Authentication 
     {
         /// <summary> 
         /// Gets or sets the path. 
         /// </summary> 
         /// <value>The path.</value> 
         public  string  Path { get ; set ; }
         /// <summary> 
         /// Gets or sets the filter attribute. 
         /// </summary> 
         /// <value>The filter attribute.</value> 
         public  string  FilterAttribute { get ; set ; }
 
         /// <summary> 
         /// Initializes a new instance of the <see cref="Authentication"/> class. 
         /// </summary> 
         /// <param name="path">The path.</param> 
         public  Authentication(string  path)
         {
             Path = path;
         }
 
         /// <summary> 
         /// Authenticates the user. 
         /// </summary> 
         /// <param name="username">The username of the user you are trying to authenticate.</param> 
         /// <param name="password">The password of the user you are trying to authenticate.</param> 
         /// <returns>true if authenticated otherwise dalse</returns> 
         public  bool  AuthenticateUser(string  username, string  password)
         {
             //Create a connection to the ldap server you want to authenticate against 
             LdapConnection  connection = new  LdapConnection (Path);
 
             //These options do not need to be set and are specific to the environment 
             var  ldapSessionOptions = connection.SessionOptions;
             ldapSessionOptions.ProtocolVersion = 3;
             ldapSessionOptions.SecureSocketLayer = true ;
 
             connection.AuthType = AuthType .Basic;
 
             //Set our connection credentials to our supplied username and password. 
             NetworkCredential  credential = new  NetworkCredential (username, password);
             connection.Credential = credential;
 
             try 
             {
                 //Check if the credentials are valid.  If they are not, the ldap connection will not bind. 
                 connection.Bind();
             }
             catch (Exception  ex)
             {
                 throw  new  Exception (ex.ToString());
             }
 
             return  true ;
         }
     }
 

Now when you want to authenticate, you just instantiate the class and then call the AuthenticateUser method supplying the username and password.

Comments

7/13/2010 10:35:26 AM #

amazon coupons

amazon coupon codes

amazon coupons United States

7/13/2010 10:42:08 AM #

amazon coupons

amazon coupon codes

amazon coupons United States

7/14/2010 5:04:51 PM #

lenen zonder bkr toetsing

U wilt geld lenen zonder BKR toetsing? De opties hiervoor worden groter, kijk verder en ontdek hoe u wél geld kunt lenen, snel & eenvoudig.

lenen zonder bkr toetsing United States

7/15/2010 4:13:23 PM #

registry cleaners

I think most people would agree with your article. I'm going to bookmark this web site so I can come back and read more posts. Keep up the good work!

registry cleaners United States

7/16/2010 11:06:51 AM #

frankfurtgebäudereinigungmain

This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. Thanks for this great resource!

frankfurtgebäudereinigungmain United States

7/17/2010 6:29:24 PM #

schlüsseldienstwiesbaden

Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

schlüsseldienstwiesbaden United States

7/20/2010 6:52:13 PM #

kaufen xbox

nsightful thoughts here. Are you certain this is the best way to look at it though? My experience is that we should pretty much live and let live because what one person thinks just -- another person simply doesn't. People are going to do what they want to do. In the end, they always do. The most we can yearn for is to highlight a few things here and there that hopefully, allows them to make just a little better informed decision. Otherwise, great post. You're definitely making me think! --Barry

kaufen xbox United States

7/24/2010 3:08:29 PM #

migraine

Migraine Gedetailleerde en berouwbare info Tips, oorzaken en behandelmethodes!

migraine United States

7/31/2010 11:58:35 PM #

lenen

Over de voor- en nadelen van het afsluiten van een lening zonder BKR-toetsing.

lenen United States

8/3/2010 2:31:34 AM #

BigTitsDolls

What is the captcha code? Please provide me captcha code codes or plugin. Thanks in advance.

BigTitsDolls United States

8/4/2010 11:00:05 AM #

ipad

It's the little things that matter, that's what I believe.

ipad United States

8/4/2010 3:00:34 PM #

Hypercom t7Plus manual

Hi. I just noticed that your site looks like it has a few code problems at the very top of your website's page. Is it an older version of Blog Engine?  I'm not sure if everybody is getting this same bugginess when browsing your blog? I am employing a totally different browser than most people, referred to as Chrome, so that is what might be causing it? I just wanted to make sure you knew. Thanks for posting some great postings and I'll try to return back with a completely different browser to check things out!

Hypercom t7Plus manual United States

8/5/2010 12:09:20 AM #

Brande Mara

Hello!
Thanks Much for giving  some creative info  on the topic.

I will definitely be sure in  saving your website and might  definitely check back sometimes.

Brande Mara United States

8/8/2010 5:46:57 AM #

meetgirls

I am speechless. This is a great blog and very engaging too. Great work! That's not really much coming from an amateur publisher like me, but it's all I could say after viewing your posts. Great grammar and vocabulary. Unlike other blogs. You really know what you're talking about too. So much that you made me want to learn more. Your blog has become a stepping stone for me, my fellow blogger. Thank you for the detailed journey. I really enjoyed the 4 posts that I have read so far.

meetgirls United States

8/10/2010 8:19:33 PM #

hypotheek

Hypotheek informatie, hypotheek aanvragen of afsluiten? Hypotheekrentes bekijken. Hypotheek aanbieders vergelijken, hypotheek vormen, bijkomende kosten,

hypotheek United States

8/29/2010 4:46:52 PM #

Jeannine

Considerably, the article is actually the greatest on this laudable topic. I harmonise with your conclusions and will eagerly look forward to your future updates. Saying thanks will not just be sufficient, for the extraordinary clarity in your writing. I will immediately grab your rss feed to stay abreast of any updates. Pleasant work and much success in your business efforts!

Jeannine United States

8/29/2010 10:59:11 PM #

student driver magnet

I was very happy to locate this site on google.I wished to say many thanks to you with regard to this good article!! I definitelyloved every little bit of it and I've you bookmarked to check out new stuff you post.

student driver magnet United States

8/30/2010 2:56:43 PM #

bail bonds baltimore

You...are...my...hero!!!  I cant believe something like this exists on the internet!  Its so true, so honest, and more than that you dont sound like an idiot!  Finally, someone who knows how to talk about a subject without sounding like a kid who didnt get that bike he wanted for Christmas.

bail bonds baltimore United States

8/31/2010 4:09:47 AM #

payroll company

Congratulations on having one of the most sophisticated blogs Ive come across in some time!  Its just incredible how much you can take away from something simply because of how visually beautiful it is.  Youve put together a great blog space --great graphics, videos, layout.  This is definitely a must-see blog!

payroll company United States

8/31/2010 6:35:52 PM #

paypal louis vuitton t-shirts

<A href="http://www.cheapclothingchina.com">cheap clothing</A>
<A href="http://www.cheapclothingchina.com">replica louis vuitton t-shirts</A>
<A href="http://www.cheapclothingchina.com">gucci jeans</A>
<A href="http://www.cheapclothingchina.com">burberry t-shirts</A>
<A href="http://www.cheapclothingchina.com">wholesale prada shoes</A>
<A href="http://www.cheapclothingchina.com">wholesale ed hardy bikini</A>
<A href="http://www.cheapclothingchina.com">christian louboutin sandals</A>
<A href="http://www.cheapclothingchina.com">cheap coach sunglasses</A>
<A href="http://www.cheapclothingchina.com">dolce gabbana handbag</A>
<A href="http://www.cheapclothingchina.com">replica nike shox r3</A>

paypal louis vuitton t-shirts People's Republic of China

8/31/2010 11:07:23 PM #

Baby Gifts, New Baby Gifts

Hi, I just found your website via Bing. Your article is truly pertinent to my life currently, and I’m really delighted I discovered your website.

Baby Gifts, New Baby Gifts United States

9/3/2010 7:26:16 AM #

gay kissing

wasll,

gay kissing Germany

Add comment


(Will show your Gravatar icon)

  Country flag


  • Comment
  • Preview
Loading