• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 832
  • 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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