• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

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.
0
forsters
Asked:
forsters
  • 4
  • 3
3 Solutions
 
Luis PérezSoftware Architect in .NetCommented:
0
 
Luis PérezSoftware Architect in .NetCommented:
By the way, in your supplied sample, you only copied the first part of the example, but you forget to copy the really important part:

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;
    }
}

If you copy this one code block, then you can do the following call:
bool test = IsExistInAD("MYDOMAIN\\MYUSER");
0
 
forstersAuthor Commented:
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?
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
Luis PérezSoftware Architect in .NetCommented:
It's normal that your page is blank, because you're not printing any output.

Try this:
   protected void Page_Load(object sender, EventArgs e)
    {
               bool test = IsExistInAD("MYDOMAIN\\MYUSER");
               if(test==true) {
                   response.Write("user exists");
                } else {
                   response.Write("user doesn't exist");
               }
    }

And, of course, replace "MYDOMAIN\\MYUSER" with real data of your domain/user.

Hope that helps.
0
 
forstersAuthor Commented:
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

0
 
Luis PérezSoftware Architect in .NetCommented:
No, you shouldn't. You should use it as this:

bool test = IsExistInAD("MYDOMAIN\\SMITH"); //for example

Anyway, you can debug your code (place a breakpoint in your first code line inside Page_Load and execute it line-by-line).
0
 
forstersAuthor Commented:
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now