forsters
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:
and then copy pasted the first code example into my code behind:
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;
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];
}
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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?
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;
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :
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 [] {'\\'});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
http://wiki.asp.net/page.aspx/462/how-to-query-active-directory-without-hard-coding-the-domain-name/
Hope so.