Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Directory Attribute List Using c#

How i get the list of active directory user attributes(not of particular user i.e.all attributes) e.g.cn,mail etc. using c#?

like image 826
mahesh Avatar asked Apr 19 '26 03:04

mahesh


2 Answers

If you're on .NET 3.5 and up, you need to check out the classes in System.DirectoryServices.ActiveDirectory for this. You need to look at classes like ActiveDirectorySchema and ActiveDirectorySchemaClass.

You can get hold of the current AD schema by using:

ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();

When you have the current schema, you can inspect the various class definitions, e.g.:

ActiveDirectorySchemaClass userSchema = currSchema.FindClass("person");

Once you have that object, you can inspect and enumerate its properties, things like:

  • MandatoryProperties
  • OptionalProperties

and so on to get an insight into the AD schema.

like image 79
marc_s Avatar answered Apr 20 '26 16:04

marc_s


DirectoryEntry dir = new DirectoryEntry();
    dir.Path = "LDAP://YourActiveDirServername ";        
    DirectorySearcher sea = new DirectorySearcher(dir);
    sea.Filter = "(sAMAccountName=Uname)";
    SearchResult seares = sea.FindOne();      
    StringBuilder str = new StringBuilder();
    System.DirectoryServices.ResultPropertyCollection prop = seares.Properties;
    ICollection coll = prop.PropertyNames;
    IEnumerator enu = coll.GetEnumerator(); 
        while (enu.MoveNext())
        {
            str.Append(enu.Current + " = " + seares.Properties[enu.Current.ToString()][0] + "\n");
        }  

Also, take a look at: http://www.codeproject.com/KB/system/everythingInAD.aspx

like image 40
Kamyar Avatar answered Apr 20 '26 15:04

Kamyar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!