Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Article

Authenticating Active Directory user in ASP.NET

Rate me:
Please Sign up or sign in to vote.
2.64/5 (20 votes)
2 May 20021 min read 426K   86   48
How to authenticate the AD user in ASP.NET by using Directory Services

Introduction

In this article I am concentrating on the validation of the Active Directory user through the ASP.NET pages and in fact you can validate the user in any sort of code (non-ASP.NET). The basic things remain the same but the implementation part will depend on the type of requirement. First of all you need to inclue the following code in the .cs file to freely use the directory services.

C#
using System.DirectoryServices;

This will allow you to get the namespace available in your code. Then to get in to the Active Directory server you need to provide the LDAP path which will find the server from the network. Now this you can will be encapsulated in the DirectoryEntry class. The following code will try to contect the server by the user name and passwords provided by you.

C#
DirectoryEntry entry = new DirectoryEntry(LDAP://ADservername,<BR>    "username","password");

As per the user name and password this will give you the abstracted property names and value pair collection. Which you can filter later to find out the information specific to the user. To get the specific information you need the DirectorySercher object which will find all the information you need in name value pairs.

C#
DirectorySearcher mySearcher = new     DirectorySearcher(entry);
    SearchResultCollection results;
    mySearcher.Filter ("name=value");
      results = mySearcher.FindAll();
e.g
mySearcher.Filter  ("cn=jignesh");

Over here I try to get the information for the user named jignesh. So for the filter string it is cn=jignesh. This is specific to Active Directory; and you should know all LDAP information about your Active Direcotry. Now is the time to rotate through the name value pair which is quite easy and which you can easily understand.

C#
foreach(SearchResult resEnt in results)
            {
                ResultPropertyCollection propcoll=resEnt.Properties;
                foreach(string key in propcoll.PropertyNames)
                {
                    foreach(object values in propcoll[key])
                    {
                        //name and value collection retrival
                    }

                }
            }

Thats it. This way you can connect with the server through the LDAP and fetch all the information from that. Like user name password etc. You can set the parameters too.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralFinding domino groups of logged in user by using asp.net Pin
superkelvin8823-Jul-09 16:32
superkelvin8823-Jul-09 16:32 
Questioncount and display users who are logedin Pin
mnalammwb25-Mar-08 1:19
mnalammwb25-Mar-08 1:19 
Questionhow to check username & password from AD Pin
asifbhura16-Jun-07 20:43
asifbhura16-Jun-07 20:43 
AnswerRe: how to check username & password from AD Pin
Govardhana Reddy2-Jul-07 20:22
professionalGovardhana Reddy2-Jul-07 20:22 
QuestionLDAPS version? Pin
sallas00019-Oct-06 20:07
sallas00019-Oct-06 20:07 
QuestionHow to list members of a group? Pin
Pete Burgess23-Apr-06 23:06
Pete Burgess23-Apr-06 23:06 
AnswerRe: How to list members of a group? Pin
Chetan Ranpariya17-Jul-06 23:56
Chetan Ranpariya17-Jul-06 23:56 
GeneralRe: How to list members of a group? Pin
Jacks Pro14-May-08 20:37
Jacks Pro14-May-08 20:37 
GeneralRe: How to list members of a group? Pin
randy_orton6469-Mar-08 1:31
randy_orton6469-Mar-08 1:31 
QuestionHow do I get the user password Pin
JP01122-Nov-05 9:52
JP01122-Nov-05 9:52 
GeneralASP vs Windows Pin
Anonymous7-Dec-04 8:22
Anonymous7-Dec-04 8:22 
GeneralRe: ASP vs Windows Pin
Ryan Fenner12-Sep-05 12:51
Ryan Fenner12-Sep-05 12:51 
Generalactive directory Pin
DgigoloB22-Nov-04 22:55
DgigoloB22-Nov-04 22:55 
Generalproblem with ldap path &quot;LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com&quot; Pin
gautam7924-Jun-04 11:00
gautam7924-Jun-04 11:00 
GeneralRe: problem with ldap path &quot;LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com&quot; Pin
mpemberton24-Jun-04 12:22
mpemberton24-Jun-04 12:22 
GeneralRe: problem with ldap path &quot;LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com&quot; Pin
lemravec29-Nov-04 9:09
lemravec29-Nov-04 9:09 
GeneralLDAP connection problems Pin
ragibhusain11-Feb-04 6:24
ragibhusain11-Feb-04 6:24 
GeneralRe: LDAP connection problems Pin
airstep28-Sep-04 7:17
airstep28-Sep-04 7:17 
GeneralRe: LDAP connection problems Pin
fdfdfdfdfdfd15-Apr-07 1:13
fdfdfdfdfdfd15-Apr-07 1:13 
GeneralFinding groups of logged in user Pin
Member 5171089-Apr-03 13:48
Member 5171089-Apr-03 13:48 
GeneralRe: Finding groups of logged in user Pin
George Vigelette3-Jun-03 3:56
George Vigelette3-Jun-03 3:56 
I wrote the following to enumerate the groups the user is a member of:
// you must have an active directory object of the root directory established
// first, then the following code should work

// set up a collection to hold the groups
StringCollection groups = new StringCollection();


DirectorySearcher srch = new DirectorySearcher(RootDirectoryEntry,"(sAMAccountName=" + strUser + ")");

// set properties to retrieve
srch.PropertiesToLoad.Add("memberOf");
srch.PropertiesToLoad.Add("sAMAccountName");

// find one entry since there should be no duplicates of sam account name
// in the active directory
SearchResult res = srch.FindOne();

// clear the groups collection for safe measure
groups.Clear();

// get the number of groups that the user is a member of
int propertyCount = res.Properties["memberOf"].Count;

// create some variables for the looping through groups
String dn;
int equalsIndex, commaIndex;

// loop through groups
for( int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)res.Properties["memberOf"][propertyCounter];

// the following is used to remove the cn= etc...
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
}
else
{
// add string to group collection
groups.Add(dn.Substring((equalsIndex + 1),(commaIndex - equalsIndex) - 1));
}
}

// now we have a string collection of all the groups this user is a member of


George Vigelette scjp mcse
gvigelet@rochester.rr.com
GeneralRe: Finding groups of logged in user Pin
Anonymous19-Jul-03 22:35
Anonymous19-Jul-03 22:35 
GeneralRe: Finding groups of logged in user Pin
Mike-EEEE16-Apr-04 6:08
Mike-EEEE16-Apr-04 6:08 
GeneralRe: Finding groups of logged in user Pin
superkelvin8823-Jul-09 16:45
superkelvin8823-Jul-09 16:45 
GeneralRe: Finding groups of logged in user Pin
eopie21-Jul-05 13:15
eopie21-Jul-05 13:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.