Click here to Skip to main content
Click here to Skip to main content

Authenticating Active Directory user in ASP.NET

By , 2 May 2002
Rate this:
Please Sign up or sign in to vote.

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.

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.

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.

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.

    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

About the Author

jignesh
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralFinding domino groups of logged in user by using asp.net Pinmembersuperkelvin8823-Jul-09 16:32 
Questioncount and display users who are logedin Pinmembermnalammwb25-Mar-08 1:19 
Questionhow to check username & password from AD Pinmemberasifbhura16-Jun-07 20:43 
AnswerRe: how to check username & password from AD PinmemberGovardhana Reddy2-Jul-07 20:22 
QuestionLDAPS version? Pinmembersallas00019-Oct-06 20:07 
QuestionHow to list members of a group? PinmemberPete Burgess23-Apr-06 23:06 
AnswerRe: How to list members of a group? PinmemberChetan Ranpariya17-Jul-06 23:56 
GeneralRe: How to list members of a group? PinmemberJacks Pro14-May-08 20:37 
GeneralRe: How to list members of a group? Pinmemberrandy_orton6469-Mar-08 1:31 
QuestionHow do I get the user password PinmemberJP01122-Nov-05 9:52 
Hello, the above code works awesome for me as long as I hard code my password into the GetGroups method call. Is there a way to grab this programatically from the NT login? I am able to grab the username by going HttpContext.Current.User.Identity.Name but I cna't seem to find something like that for the password. I have never attempted anything using AD before so I am a little lost and confused. Thanks in advance for your help.
 
JP011
 
-- modified at 16:21 Tuesday 22nd November, 2005
GeneralASP vs Windows PinsussAnonymous7-Dec-04 8:22 
GeneralRe: ASP vs Windows PinmemberRyan Fenner12-Sep-05 12:51 
Generalactive directory PinmemberDgigoloB22-Nov-04 22:55 
Generalproblem with ldap path &quot;LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com&quot; Pinmembergautam7924-Jun-04 11:00 
GeneralRe: problem with ldap path &quot;LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com&quot; Pinmembermpemberton24-Jun-04 12:22 
GeneralRe: problem with ldap path &quot;LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com&quot; Pinmemberlemravec29-Nov-04 9:09 
GeneralLDAP connection problems Pinmemberragibhusain11-Feb-04 6:24 
GeneralRe: LDAP connection problems Pinmemberairstep28-Sep-04 7:17 
GeneralRe: LDAP connection problems Pinmemberfdfdfdfdfdfd15-Apr-07 1:13 
GeneralFinding groups of logged in user Pinsussrdever49-Apr-03 13:48 
GeneralRe: Finding groups of logged in user PinmemberGeorge Vigelette3-Jun-03 3:56 
GeneralRe: Finding groups of logged in user PinsussAnonymous19-Jul-03 22:35 
GeneralRe: Finding groups of logged in user PinmemberMike-EEEE16-Apr-04 6:08 
GeneralRe: Finding groups of logged in user Pinmembersuperkelvin8823-Jul-09 16:45 
GeneralRe: Finding groups of logged in user Pinmembereopie21-Jul-05 13:15 
GeneralRe: Finding groups of logged in user PinsussShrijeet3-Aug-05 11:15 
GeneralRe: Finding groups of logged in user [modified] Pinmembervincennes22-Oct-12 4:41 
GeneralIm trying to figure out a code ... PinmemberJeremyDeal18-Dec-02 8:18 
Questionhow can you tell if the userid/password is valid Pinmemberssue10-Dec-02 7:54 
AnswerRe: how can you tell if the userid/password is valid Pinmemberg.fiorentini9-May-03 5:14 
GeneralClient certificate Pinmemberadk19-Nov-02 22:30 
Generalbookbik_thailand PinsussAnonymous11-Sep-02 23:51 
Generalprivlages... PinmemberRay Cassick11-Sep-02 10:39 
GeneralRe: privlages... PinmemberLMenchu16-Nov-04 11:36 
QuestionAnd without AD? PinsussAnonymous11-Sep-02 9:53 
AnswerRe: And without AD? PinsussAnonymous19-Mar-03 5:29 
GeneralOther fields... PinmemberElvis Fernández10-Aug-02 6:51 
GeneralRe: Other fields... PinmemberMikDelaney21-Feb-03 5:16 
GeneralRe: Other fields... PinmemberSuvadip De17-Aug-04 1:54 
GeneralRe: Other fields... Pinmemberlemravec29-Nov-04 9:02 
GeneralRe: Other fields... Pinsussnorai2-May-05 7:32 
GeneralI got a Doubt... PinmemberElvis Fernández1-Aug-02 9:27 
GeneralRe: I got a Doubt... PinsussRyan Kelley10-Aug-02 3:49 
GeneralRe: I got a Doubt... PinmemberElvis Fernández10-Aug-02 6:49 
GeneralNice Article PinmemberNick Parker4-May-02 1:38 
GeneralRe: Nice Article PinmemberDato7-Feb-03 9:39 
GeneralRe: Nice Article PinsussAnonymous24-Feb-03 14:44 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 3 May 2002
Article Copyright 2002 by jignesh
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid