---
title: How to Retrieve Active Directory Information for Authentication in ASP.NET
description: How to Retrieve Active Directory Information for Authentication in ASP.NET
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [News](https://blog.e-zest.com/news)
- [Podcasts](https://www.e-zest.com/podcasts)
- [Healthcare](https://blog.e-zest.com/healthcare)
- [CSR](https://blog.e-zest.com/csr)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [Media Kit](https://www.e-zest.com/media-kit)

# How to Retrieve Active Directory Information for Authentication in ASP.NET

[Swapna Patne](https://blog.e-zest.com/author/swapna-patne)  Jan 14, 2013

[Active Directories](https://blog.e-zest.com/topic/active-directories) [Lightweight Directory Access Protocol](https://blog.e-zest.com/topic/lightweight-directory-access-protocol) [C#.NET](https://blog.e-zest.com/topic/c-net) [LDAP](https://blog.e-zest.com/topic/ldap) [ASP.NET](https://blog.e-zest.com/topic/asp-net) [Microsoft .Net](https://blog.e-zest.com/topic/microsoft-net-2) [Technology](https://blog.e-zest.com/topic/technology)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/&text=How%20to%20Retrieve%20Active%20Directory%20Information%20for%20Authentication%20in%20ASP.NET>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/>

**Introduction**

Hello Friends, I would like to share some of my interesting findings for retrieving the Active Directory information for authentication in web application using C#.NET.

So let’s move on to the implementation details.

Life has been made easy with the introduction of the `System.DirectoryServices.dll` library in .Net Framework. There's a simple function which I have written that does the Forms/Window authentication to permit users to authenticate against Active Directory using the Lightweight Directory Access Protocol (LDAP).

This custom function basically takes a username, and tries to authenticate it on the given LDAP (Active Directory).

The code below demonstrates the same.

Add the reference to the `System.DirectoryServices.dll`. Then, include the library in your page:

`using System.DirectoryServices;`

I have created a custom static class for accessing all Active Directory properties

```

public static class ADProperties
{
	public const string ACCOUNTEXPIRES = "accountExpires";
	public const string ADMINCOUNT = "adminCount";
	public const string BADPASSWORDTIME = "badPasswordTime";
	public const string BADPWDCOUNT = "badPwdCount";
	public const string CITY = "l";
	public const string CODEPAGE = "codePage";
	public const string COMPANY = "company";
	public const string CONTAINERNAME = "cn";
	public const string COUNTRY = "co";
	public const string COUNTRYCODE = "countryCode";
	public const string COUNTRYNOTATION = "c";
	public const string DEPARTMENT = "department";
	public const string DIRECTREPORTS = "directReports";
	public const string DISPLAYNAME = "displayName";
	public const string DISTINGUISHEDNAME = "distinguishedName";
	public const string DSCOREPROPAGATIONDATA = "dSCorePropagationData";
	public const string EMAILADDRESS = "mail";
	public const string EXTENSION = "ipPhone";
	public const string FAX = "facsimileTelephoneNumber";
	public const string FIRSTNAME = "givenName";
	public const string HOMEMDB = "homeMDB";
	public const string HOMEMTA = "homeMTA";
	public const string HOMEPHONE = "homePhone";
	public const string INSTANCETYPE = "instanceType";
	public const string LASTLOGOFF = "lastLogoff";
	public const string LASTLOGON = "lastLogon";
	public const string LASTLOGONTIMESTAMP = "lastLogonTimestamp";
	public const string LASTNAME = "sn";
	public const string LEGACYEXCHANGEDN = "legacyExchangeDN";
	public const string LOGINNAME = "sAMAccountName";
	public const string LOGONCOUNT = "logonCount";
	public const string MAILNICKNAME = "mailNickname";
	public const string MANAGER = "manager";
	public const string MDBUSEDEFAULTS = "mDBUseDefaults";
	public const string MEMBEROF = "memberOf";
	public const string MIDDLENAME = "initials";
	public const string MOBILE = "mobile";
	public const string MSEXCHHOMESERVERNAME = "msExchHomeServerName";
	public const string MSEXCHMAILBOXGUID = "msExchMailboxGuid";
	public const string MSEXCHMAILBOXSECURITYDESCRIPTOR = "msExchMailboxSecurityDescriptor";
	public const string MSEXCHPOLICIESINCLUDED = "msExchPoliciesIncluded";
	public const string MSEXCHRECIPIENTDISPLAYTYPE = "msExchRecipientDisplayType";
	public const string MSEXCHRECIPIENTTYPEDETAILS = "msExchRecipientTypeDetails";
	public const string MSEXCHUSERACCOUNTCONTROL = "msExchUserAccountControl";
	public const string MSEXCHVERSION = "msExchVersion";
	public const string NAME = "name";
	public const string NTSECURITYDESCRIPTOR = "nTSecurityDescriptor";
	public const string OBJECTCATEGORY = "objectCategory";
	public const string OBJECTCLASS = "objectClass";
	public const string OBJECTGUID = "objectGUID";
	public const string OBJECTSID = "objectSid";
	public const string PAGER = "pager";
	public const string PHYSICALDELIVERYOFFICENAME = "physicalDeliveryOfficeName";
	public const string POSTALCODE = "postalCode";
	public const string PRIMARYGROUPID = "primaryGroupID";
	public const string PROXYADDRESSES = "proxyAddresses";
	public const string PWDLASTSET = "pwdLastSet";
	public const string SAMACCOUNTTYPE = "sAMAccountType";
	public const string SERVICEPRINCIPALNAME = "servicePrincipalName";
	public const string SHOWINADDRESSBOOK = "showInAddressBook";
	public const string STATE = "st";
	public const string STREETADDRESS = "streetAddress";
	public const string TITLE = "title";
	public const string USERACCOUNTCONTROL = "userAccountControl";
	public const string USERPRINCIPALNAME = "userPrincipalName";
	public const string USNCHANGED = "uSNChanged";
	public const string USNCREATED = "uSNCreated";
	public const string WHENCHANGED = "whenChanged";
	public const string WHENCREATED = "whenCreated";
}
```

You can use the following function along with forms based authentication or it can used just to check a user's credentials. It takes the following input variables:

username: The user's account name. It can be prefixed by the domain;

e.g., mydomaintom or just username ex: tom..

```

public void GetActiveDirectoryDetails(string username)
    {
DirectorySearcherdirectorysearcher = new DirectorySearcher();
        var path = directorysearcher.SearchRoot.Path;
        DirectoryEntry dirEntry = new DirectoryEntry(path);
        DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry) { Filter = "(" + ADProperties.LOGINNAME + "=" + username+ ")" };

        SearchResult result = dirSearcher.FindOne();

        if (result != null)
        {
            if (result.Properties.Contains(ADProperties.FIRSTNAME))
            {
                firstName = Convert.ToString(result.Properties[ADProperties.FIRSTNAME][0]);
            }
            if (result.Properties.Contains(ADProperties.LASTNAME))
            {
                lastName = Convert.ToString(result.Properties[ADProperties.LASTNAME][0]);
            }
            if (result.Properties.Contains(ADProperties.EMAILADDRESS))
            {
                emailId = Convert.ToString(result.Properties[ADProperties.EMAILADDRESS][0]);
            }
            if (result.Properties.Contains(ADProperties.DEPARTMENT))
            {
                department = Convert.ToString(result.Properties[ADProperties.DEPARTMENT][0]);
            }

        }
    }
```

The below code will help you to fetch the Groups from the Active Directory

```

public string GetGroups(string param)
{
    DirectorySearcher directorysearcher = new DirectorySearcher();
    var path = directorysearcher.SearchRoot.Path;

    DirectoryEntry dirEntry = newDirectoryEntry(path);
    DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry) { Filter = "(" + ADProperties.LOGINNAME + "=" + param + ")" };

    SearchResult result = dirSearcher.FindOne();
    StringBuildergroupNames = new StringBuilder();

    try
    {

        int propertyCount = result.Properties["memberOf"].Count;
        string dn;
        int equalsIndex, commaIndex;

        for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
        {
            dn = (string)result.Properties["memberOf"][propertyCounter];
            equalsIndex = dn.IndexOf("=", 1);
            commaIndex = dn.IndexOf(",", 1);
            if (-1 == equalsIndex)
            {
                return null;
            }
            groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
            groupNames.Append("|");
        }
    }
    catch (Exception ex)
    {
        throw new Exception("Error obtaining group names. " + ex.Message);
    }

	return groupNames.ToString();
}
```

So friends, I hope this will help you to get all Active Directories in a single custom file.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/&text=How%20to%20Retrieve%20Active%20Directory%20Information%20for%20Authentication%20in%20ASP.NET>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/how-to-retrieve-active-directory-information-for-authentication-in-asp-net/&title=&summary=&source=>

### Similar Blog

[![How to read the spreadsheet (\*.xls,\*.xlxs and \*.csv) and import into dataset using DotNet](https://blog.e-zest.com/hubfs/How%20to%20read%20%20the%20spreadsheet%20and%20%20import%20into%20dataset%20%20using%20DotNet%20.jpg) How to read the spreadsheet (\*.xls,\*.xlxs and \*.csv) and import into dataset using DotNet **Introduction** In my previous blog, i shared details on how to generate the spreadsheet dynamically without using Microsoft ... By Swapna Patne Nov 16, 2017](https://blog.e-zest.com/how-to-read-the-spreadsheet-.xls.xlxs-and-.csv-and-import-into-dataset-using-dotnet)

[![How to generate the dynamic spreadsheet in DotNet](https://blog.e-zest.com/hubfs/How%20to%20generate%20the%20dynamic%20spreadsheet%20in%20DotNet_.jpg) How to generate the dynamic spreadsheet in DotNet **Introduction** In this blog, I would like to share some of my interesting findings to generate the spreadsheet dynamically ... By Swapna Patne Nov 01, 2017](https://blog.e-zest.com/how-to-generate-the-dynamic-spreadsheet-in-dotnet)

[![How to Implement SQL Server Replication](https://blog.e-zest.com/hs-fs/file-3395401939-png/blog-files/advantages-of-replications.png) How to Implement SQL Server Replication SQL Server replication is a set of technologies for copying and distributing data and database objects from one database to ... By Swapna Patne Feb 13, 2015](https://blog.e-zest.com/how-to-implement-sql-server-replication/)

[How to improve the performance of the application in ASP .Net **Introduction** By Swapna Patne Aug 25, 2014](https://blog.e-zest.com/how-to-improve-the-performance-of-the-application-in-asp/)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.