Skip to content

ASP.NET (C#) & Active Directory

Simon

Asked Oct 9, 2006, 12:31 AM EDT7 answers
Share
LinkedInX
I use the Example of this Page (http://www.c-sharpcorner.com/Code/2005/June/ADand.NET.asp). But the Point 3 doen't work.

3. Validate if a user exists
///
/// Method to validate if a user exists in the AD.
///
///
///
public bool UserExists(string UserName){
DirectoryEntry de = ADHelper.GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot =de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName +"))";
SearchResultCollection results = deSearch.FindAll();
if(results.Count == 0){
return false;
}
else{
return true;
}
}

In the Code is a Class ADHelper, but this Class dosen't exist. Is this Example not finished or what is the problem?
I really need this Script. I have a Project in my company. To say is, that i'm a Noobie. My question is maybe stupid, but i hope you can help me?

Thank you very much!!
Greetz Simon

7 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    The answer is in the first two examples, just put all of the functions in the same class and leave off the ADHelper part, just call the thread like this:

    public static DirectoryEntry GetDirectoryEntry()
    {
       DirectoryEntry de = new DirectoryEntry();
       de.Path = "LDAP://192.168.1.1/CN=Users;DC=Yourdomain";
       de.AuthenticationType = AuthenticationTypes.Secure;
       return de;
    }

    public bool UserExists(string UserName)
       {
       DirectoryEntry de = GetDirectoryEntry();
       DirectorySearcher deSearch = new DirectorySearcher();
       deSearch.SearchRoot =de;
       deSearch.Filter = "(&(objectClass=user) (cn=" + UserName +"))"; 
       SearchResultCollection results = deSearch.FindAll();
       if(results.Count == 0)
       {
          return false;
       }
       else
       {
          return true;
       }
    }

    The GetDirectoryEntry() method is declared in examples one and two (one explicitly sets the authentication and the other uses the logged on user to authenticate), so you can just use one of those methods (whichever is appropriate for you) to get a root entry.

    Later
    Mark
    0
  • 0
    thank you very much!
    it works very good!

    Anyway, do you know a list, how can i speak to a user account properties.
    For examle: when i will change the Street or the e-mail or office or the notes etc.??

    0
  • 0
    What I would do is loop through each element in the Properties collection and print out the name, for example if you go to this website:

    http://geekswithblogs.net/mhamilton/archive/2005/10/04/55920.aspx

    Especially pay attention to the bottom part where he explains how to list all of the properties available in your directory. 

    Good luck.
    Mark
    0
  • 0
    Okay it works, but i became only

    objectClass
    ou
    distinguishedName
    nstanceType
    whenCreated
    whenChanged
    name
    objectGUID
    objectCategory
    nTSecurityDescritor

    Where are the properties from the Account like accountname, first-, lastname, street, country, etc. ?
    Anybody knows? Thank you very much!

    0
  • 0
    My guess, just a wild one, is that the object you are looking at the properties for is not a user object, but might be an OU, perhaps the root?  Try getting ahold of a user object and looping through the properties of that. 

    Later
    Mark
    0
  • 0
    Okay i see it. I have in the class "SetProperty" the DirectoryEntry Object from the secure Connection "de" given.
    But which Object must I transfer? or how can i have the User as a directoryEntry, becauce in the class "SetProperty" I must transfer a Directory Entry Object..

    Here ist the Code. It's a bit long, but when you include the code in your editor, is not that the problem.

    I thank you very much for help!!


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.DirectoryServices;
    using System.Globalization;
    using System.Security.Principal;
    using System.Web.Services.Description;
    //using DirectoryUtility.DirectoryServices;


    //namespace DirectoryUtility


    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    GetDirectoryEntry();
    UserExists(textbox_user.Text);

    }


    //1. Create a connection to Active Directory
    ///
    /// Method used to create an entry to the AD.
    /// Replace the path, username, and password.
    ///
    /// DirectoryEntry
    public static DirectoryEntry GetDirectoryEntry()
    {
    DirectoryEntry de = new DirectoryEntry();
    //de.Path = "LDAP://192.168.1.1/CN=WebIDA;DC=Main";
    de.Path = "LDAP://sv01/OU=WebIDA,DC=Main,DC=local";
    de.Username = @"Main\programmer";
    de.Password = "123asd$";
    de.AuthenticationType = AuthenticationTypes.Secure;
    return de;
    }

    //2. Create a secure connection to Active Directory

    ///
    /// Establish identity (principal) and culture for a thread.
    ///
    /*public static void SetCultureAndIdentity()
    {
    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
    WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
    WindowsIdentity identity = (WindowsIdentity)principal.Identity;
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    }*/

    //3. Validate if a user exists

    ///
    /// Method to validate if a user exists in the AD.
    ///
    ///
    ///
    public bool UserExists(string UserName)
    {
    DirectoryEntry de = GetDirectoryEntry();
    DirectorySearcher deSearch = new DirectorySearcher();
    deSearch.SearchRoot = de;
    deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
    SearchResultCollection results = deSearch.FindAll();

    if (results.Count == 0)
    {
    //test_button.Visible = false;
    Label1.Text = "User besteht nicht!";
    return false;
    }
    else
    {

    //List all AD-account Properties
    PopulateProperties(de);

    //test_button.Visible = true;
    Label1.Text = "User besteht";
    return true;


    }
    }
    //4. Set user's properties

    ///
    /// Helper method that sets properties for AD users.
    ///
    ///
    ///
    ///
    public static void SetProperty(DirectoryEntry de, string PropertyName, string PropertyValue)
    {
    if (PropertyValue != null)
    {
    if (de.Properties.Contains(PropertyName))
    {
    de.Properties[PropertyName][0] = PropertyValue;
    }
    else
    {
    de.Properties[PropertyName].Add(PropertyValue);
    }
    }
    }

    //gibt die properties des accounts userenty aus
    public void PopulateProperties(DirectoryEntry userEntry)
    {
    if (userEntry != null)
    {
    //propertiesListBox.clear();
    foreach (string propertyName in userEntry.Properties.PropertyNames)
    {
    //string propertyName = userEntry.Password.ToString;
    propertiesListBox.Items.Add(propertyName);

    //Property Ausgabe (Listbox)
    PropertyOutput(userEntry, propertyName);
    }
    }
    }

    //gibt den Wert der Properties ind PopulateProperties aus
    public void PropertyOutput(DirectoryEntry de, string propertyName)
    {
    //string loginID = de.Properties["sAMAccountName"].Value.ToString();
    string strPropertie = de.Properties[propertyName].Value.ToString();
    //MessageBox.Show("Login ID: " + loginID.ToString() + "\r\n Full Name " + firstName.ToString(), "Results");

    ListBox1.Items.Add(strPropertie);
    }

    }
    0
  • 0
    " Try getting ahold of a user object and looping through the properties of that. "

    How can i do this....???? please help me!
    I searched the hole Web for this, but it doesn't work....

    how can i get a user Object to change the properties of a user?? This is my last question, but i need this!
    0