Click here to Skip to main content
6,305,776 members and growing! (15,150 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Authenticating Active Directory user in ASP.NET

By jignesh

How to authenticate the AD user in ASP.NET by using Directory Services
C#, .NET, Win2K, WinXP, ASP.NET, Visual Studio, Dev
Posted:29 Apr 2002
Updated:2 May 2002
Views:268,639
Bookmarked:72 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
19 votes for this article.
Popularity: 3.47 Rating: 2.71 out of 5
5 votes, 31.3%
1
3 votes, 18.8%
2
2 votes, 12.5%
3
4 votes, 25.0%
4
2 votes, 12.5%
5

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,
"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


Member

Occupation: Web Developer
Location: United States United States

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
Questioncount and display users who are logedin Pinmembermnalammwb2:19 25 Mar '08  
Generalhow to check username & password from AD Pinmemberasifbhura21:43 16 Jun '07  
GeneralRe: how to check username & password from AD PinmemberGovardhana Reddy21:22 2 Jul '07  
QuestionLDAPS version? Pinmembersallas000121:07 9 Oct '06  
QuestionHow to list members of a group? PinmemberPete Burgess0:06 24 Apr '06  
AnswerRe: How to list members of a group? PinmemberChetan Ranpariya0:56 18 Jul '06  
GeneralRe: How to list members of a group? PinmemberJacks Pro21:37 14 May '08  
GeneralRe: How to list members of a group? Pinmemberrandy_orton6462:31 9 Mar '08  
QuestionHow do I get the user password PinmemberJP01110:52 22 Nov '05  
GeneralASP vs Windows PinsussAnonymous9:22 7 Dec '04  
GeneralRe: ASP vs Windows PinmemberRyan Fenner13:51 12 Sep '05  
Generalactive directory PinmemberDgigoloB23:55 22 Nov '04  
Generalproblem with ldap path "LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com" Pinmembergautam7912:00 24 Jun '04  
GeneralRe: problem with ldap path "LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com" Pinmembermpemberton13:22 24 Jun '04  
GeneralRe: problem with ldap path "LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com" Pinmemberlemravec10:09 29 Nov '04  
GeneralLDAP connection problems Pinmemberragibhusain7:24 11 Feb '04  
GeneralRe: LDAP connection problems Pinmemberairstep8:17 28 Sep '04  
GeneralRe: LDAP connection problems Pinmemberfdfdfdfdfdfd2:13 15 Apr '07  
GeneralFinding groups of logged in user Pinsussrdever414:48 9 Apr '03  
GeneralRe: Finding groups of logged in user PinmemberGeorge Vigelette4:56 3 Jun '03  
GeneralRe: Finding groups of logged in user PinsussAnonymous23:35 19 Jul '03  
GeneralRe: Finding groups of logged in user PinmemberMike-EEEE7:08 16 Apr '04  
GeneralRe: Finding groups of logged in user Pinmembereopie14:15 21 Jul '05  
GeneralRe: Finding groups of logged in user PinsussShrijeet12:15 3 Aug '05  
GeneralIm trying to figure out a code ... PinmemberJeremyDeal9:18 18 Dec '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2002
Editor: Nishant Sivakumar
Copyright 2002 by jignesh
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project