Matt Paulson

Entrepreneur, Author, Private Equity Investor
  • Facebook
  • Linkedin
  • Twitter
  • Rss
  • Blog
    • Book Recommendations
    • Church Life & Ministry
    • Community Projects
    • Entrepreneurship
    • Quaterly Updates
    • Startup Community
    • Startup Q&A Show
    • Web Development
  • About
    • Resume
  • My Companies
    • Angel Investments
  • My Books
    • Online Business from Scratch
    • Automatic Income
    • The Ten-Year Turnaround
    • Email Marketing Demystified
    • 40 Rules for Internet Business Success
    • Business Growth Day by Day
    • Simple Savings
  • Media Appearances
  • Contact
Home» Web Development » How to Authenticate a User in Active Directory using ASP.NET

How to Authenticate a User in Active Directory using ASP.NET

Posted on June 2, 2009 by matt in Web Development 7 Comments
Tweet

If you’re working in an academic or large corporate or government setting, changes are you’re going to have a network in place using Active Directory or an open-source equivalent. Every user in the organization will have some sort of an account to use. If you’re building an internal web-application or desktop-application, it doesn’t make a lot of sense to give the user another set of credentials. Instead, you can validate users by checking the permissions existing Active Directory accounts.

The source code to check a user’s credentials in Active Directory using C# or Visual Basic is actually fairly minimal. This works with both ASP.NET and with Windows Forms  (or WPF for that matter) if you’re building a desktop application.

Here’s how to do it:

(1) Reference the appropriate library

You’ll need to make use of the System.DirectoryServices library that comes with Visual Studio. You can add this to your ASP.NET code-behind page or your C# class for your Windows forms like this.

using System.DirectoryServices;

(2) Create An Authentication Function.

Here’s a basic function that will check a user’s permissions on a given domain. Essentially, it will try to create an Active Directory entry using the provided credentials, and it can successfully create a valid entry, we know that the user is authenticated. Otherwise, it’ll return false.

public bool AuthenticateActiveDirectory(string Domain, string UserName, string Password)
{
try
{
DirectoryEntry entry = new DirectoryEntry(“LDAP://” + Domain, UserName, Password);
object nativeObject = entry.NativeObject;
return true;
}
catch (DirectoryServicesCOMException) { return false; }
}

That’s really all there is to it. Microsoft has an extensive aritcle on MSDN that covers active directory authentication in .NET that you might want to check out as well.

Tweet

Comments

7 comments on “How to Authenticate a User in Active Directory using ASP.NET”

  1. Kyle says:
    June 2, 2009 at 9:43 pm

    This is awesome and will come in handy. I'm usually lazy and just use IIS to handle the AD authentication side.
    Now that you've been able to authenticate to AD, how hard would it be to create a function that allows a user to change their password?

    Reply
    • Matthew Paulson says:
      June 3, 2009 at 7:30 am

      It's actually not that difficult at all. You just create a DirectoryEntry object and use the .Invoke method to change the password.

      Here's a link to some example code:
      http://www.primaryobjects.com/CMS/Article66.aspx

      Reply
  2. Adam says:
    August 6, 2009 at 10:47 am

    Just what I needed. Thanks for the tutorial.

    Reply
  3. Mohmmad says:
    December 23, 2009 at 3:46 am

    That's very good function and work very welll. Thanks to put a usful article. tanks again

    Reply
  4. Authentication with Active Directory from Forms Authentication « Ernesto Ocampo’s Blog says:
    February 15, 2010 at 3:30 pm

    […] http://msdn.microsoft.com/en-us/library/ms180890(VS.80).aspx http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-usi… http://support.microsoft.com/kb/180548/en-us […]

    Reply
  5. Autenticación con Active Directory y Forms Authentication « Ernesto Ocampo Blog en Español says:
    March 25, 2010 at 10:09 am

    […] http://msdn.microsoft.com/en-us/library/ms180890(VS.80).aspx http://www.adventuresindevelopment.com/2009/06/02/how-to-authenticate-a-user-in-active-directory-usi… http://support.microsoft.com/kb/180548/en-us […]

    Reply
  6. Sara B. says:
    September 4, 2012 at 5:05 pm

    Do you know if this still works now that there are newer versions of ASP.Net? Or if there is an easier way of authenticating/authorizing users for an intranet?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Email Updates

Enter your email address below to receive a steady stream of tricks, tips and ideas to help you build a better and more profitable business.

Email Marketing Demystified

The second edition of Email Marketing Demystified is now available. This book teaches you how to build a massive email list, write marketing copy that converts and generate more sales in your business.

Click Here to Get Your Copy of Email Marketing Demystified

Recent Posts

  • Leadership Sioux Falls is Fundraising for New Playground Equipment at Hayward Park. Here’s How You Can Help.

    March 2, 2021
  • Sioux Falls to Celebrate Entrepreneurship Day on March 3, 2021

    February 18, 2021
  • MarketBeat is Hiring for Three Positions (Support, Data Analyst and Web Developer)

    February 17, 2021
  • The Biggest Mistake You Can Make with Your Email Sign-Up Forms

    February 16, 2021
  • Five Lessons Learned from the MarketBeat Burger Project

    February 2, 2021

    Follow me on Facebook & YouTube

    Contact

    • [email protected]
    • Contact Us
    • matthew-paulson
      • Facebook
      • Twitter
      • Linkedin
      • Instagram
      • Rss

    © Matthew Paulson 2003-2021. All Rights Reserved.

    Privacy Policy