Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Query Active Directory in asp.net c#

Hi Experts,

trying to learn about querying AD from an asp.net c# web page.

Never done this before, so started googling, tried to follow an example online:
http://www.codeproject.com/Articles/9570/Querying-Microsoft-Active-Directory-Using-Microsof

so I added:

using System.DirectoryServices;

Open in new window


and then copy pasted the first code example into my code behind:

string ExtractUserName(string path)
{
    string [] userPath = path.Split(new char [] {'\\'});
    return userPath[userPath.Length-1];
}

Open in new window

I'm obviously being really stupid, the tutorial suggests that from this we now know the username, but I don't seem to get anything. Can someone point me in the direction of a more idiot proof tutorial, or help me out, I only want to query AD don't need to write to it at this stage.
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of forsters

ASKER

Roland Thank you,

I had started reading your link but haven't got very far.

Re. you second comment I now have the following in my code-behind:

using System;
using System.DirectoryServices;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
               bool test = IsExistInAD("MYDOMAIN\\MYUSER");
    }
    string ExtractUserName(string path)
{
    string [] userPath = path.Split(new char [] {'\\'});
    return userPath[userPath.Length-1];
}

    bool IsExistInAD(string loginName)
    {
        string userName = ExtractUserName(loginName);
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format("(SAMAccountName={0})", userName);
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();

        if (result == null)
        {
            return false;
        }
        else
        {
            return true;
        }
        
       
    }
      
}

Open in new window


But if I run the page, it's blank, so i'm obviously missing something obvious to those in the know...can you see what?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
D'oh of course, I was confusing return with response.write - good start...THANK YOU (sorry still a c# newbie too...easily confused)!

Hmm so apparently 'user doesn't exist'.

I wasn't sure - the code above is exactly what I am running, normally where text is to be replaced with actual values it's held in square brackets - in the absence of those I have run literally the code I posted, should I be populating the empty square brackets in :

string [] userPath = path.Split(new char [] {'\\'});

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh I see, ok got it great that works.

Brilliant thanks, I'll go and read your attachment now, see how far I get before I'm back on here with another newbie question.

Thank you so much.