Click here to Skip to main content
Licence 
First Posted 29 Apr 2002
Views 334,612
Bookmarked 84 times

Authenticating Active Directory user in ASP.NET

By | 2 May 2002 | Article
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.

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

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFinding domino groups of logged in user by using asp.net Pinmembersuperkelvin8816:32 23 Jul '09  
Questioncount and display users who are logedin Pinmembermnalammwb1:19 25 Mar '08  
Questionhow to check username & password from AD Pinmemberasifbhura20:43 16 Jun '07  
AnswerRe: how to check username & password from AD PinmemberGovardhana Reddy20:22 2 Jul '07  
QuestionLDAPS version? Pinmembersallas000120:07 9 Oct '06  
QuestionHow to list members of a group? PinmemberPete Burgess23:06 23 Apr '06  
AnswerRe: How to list members of a group? PinmemberChetan Ranpariya23:56 17 Jul '06  
arl = new ArrayList();
arl = GetAllADDomainUsers("LDAP://in.QUEST-SOFTWARE.com");
for (int i = 0; i < arl.Count; i++)
{
lstUsersName.Items.Add(arl[i].ToString());
}
 
ArrayList GetAllADDomainUsers(string domainpath)
{
//domainpath = "LDAP://in.Quest-Software.com";
ArrayList allUsers = new ArrayList();
sb = new System.Text.StringBuilder();
sbProperty = new System.Text.StringBuilder();
searchRoot = new DirectoryEntry(domainpath);
search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("desacription");
search.PropertiesToLoad.Add("givenName");
// search.PropertiesToLoad.Add("userPassword");

resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains("samaccountname"))
{
allUsers.Add((String)result.Properties["samaccountname"][0]);
}
}

properties = searchRoot.Properties;
foreach (string name in properties.PropertyNames)
{
foreach (object obj in properties[name])
{
sb.Append(name + "\t: " + obj.ToString());
sb.AppendLine();
}
}
}
return allUsers;
}
GeneralRe: How to list members of a group? PinmemberJacks Pro20:37 14 May '08  
GeneralRe: How to list members of a group? Pinmemberrandy_orton6461:31 9 Mar '08  
QuestionHow do I get the user password PinmemberJP0119:52 22 Nov '05  
GeneralASP vs Windows PinsussAnonymous8:22 7 Dec '04  
GeneralRe: ASP vs Windows PinmemberRyan Fenner12:51 12 Sep '05  
Generalactive directory PinmemberDgigoloB22:55 22 Nov '04  
Generalproblem with ldap path "LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com" Pinmembergautam7911:00 24 Jun '04  
GeneralRe: problem with ldap path "LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com" Pinmembermpemberton12:22 24 Jun '04  
GeneralRe: problem with ldap path "LDAP://DC=onecity,DC=corp,DC=fabrikam,DC=com" Pinmemberlemravec9:09 29 Nov '04  
GeneralLDAP connection problems Pinmemberragibhusain6:24 11 Feb '04  
GeneralRe: LDAP connection problems Pinmemberairstep7:17 28 Sep '04  
GeneralRe: LDAP connection problems Pinmemberfdfdfdfdfdfd1:13 15 Apr '07  
GeneralFinding groups of logged in user Pinsussrdever413:48 9 Apr '03  
GeneralRe: Finding groups of logged in user PinmemberGeorge Vigelette3:56 3 Jun '03  
GeneralRe: Finding groups of logged in user PinsussAnonymous22:35 19 Jul '03  
GeneralRe: Finding groups of logged in user PinmemberMike-EEEE6:08 16 Apr '04  
GeneralRe: Finding groups of logged in user Pinmembersuperkelvin8816:45 23 Jul '09  
GeneralRe: Finding groups of logged in user Pinmembereopie13:15 21 Jul '05  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 3 May 2002
Article Copyright 2002 by jignesh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid