Active Directory Reading and updating Properties

Here I am created a class File lke bleow

 public class Userdetails
{
    public static string ADPath = ConfigurationManager.AppSettings.Get(“ADPath”); // Get the ADAM Path from web config fiel
    public static string ADUser = ConfigurationManager.AppSettings.Get(“ADUser”); //ADAM Administrator
    public static string ADPassword = ConfigurationManager.AppSettings.Get(“ADPassword”); //ADAM Administrator  password

    public static DirectoryEntry GetUserDetails(string userID)
    {
            AuthenticationTypes AuthTypes;  // Authentication flags.
            // Set authentication flags.
            // For non-secure connection, use LDAP port and
            //  ADS_USE_SIGNING |
            //  ADS_USE_SEALING |
            //  ADS_SECURE_AUTHENTICATION
            // For secure connection, use SSL port and
            //  ADS_USE_SSL | ADS_SECURE_AUTHENTICATION
            AuthTypes = AuthenticationTypes.Signing |
                AuthenticationTypes.Sealing |
                AuthenticationTypes.Secure;
            DirectoryEntry De = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthTypes);
        DirectorySearcher Ds = new DirectorySearcher(De);
        SearchResult Sr;
        Ds.SearchScope = SearchScope.Subtree;
        Ds.Filter = “(&(objectclass=*)(cn= ” + userID + “))”;
        Sr = Ds.FindOne();
        if (!(Sr == null))
        {
            De = new DirectoryEntry(Sr.Path, ADUser, ADPassword, AuthTypes);
            return De;
        }
        else
        {
            return null;
        }
    }

//Updating the ADAM password.   

 public static void SetUserPassword(DirectoryEntry oDE, string Password)
    {
        int intPort = int.Parse(“389”);
        const long ADS_OPTION_PASSWORD_PORTNUMBER = 6;
        const long ADS_OPTION_PASSWORD_METHOD = 7;
        const int ADS_PASSWORD_ENCODE_REQUIRE_SSL = 0;
        const int ADS_PASSWORD_ENCODE_CLEAR = 1;

        oDE.Invoke(“SetOption”, new object[] { ADS_OPTION_PASSWORD_PORTNUMBER, intPort });
       oDE.Invoke(“SetOption”, new object[] {ADS_OPTION_PASSWORD_METHOD, ADS_PASSWORD_ENCODE_CLEAR});
        oDE.Invoke(“SetPassword”, new object[] { Password });
    }

//Getting the ADAM Properties
    public static string GetProperty(DirectoryEntry De, string PropertyName)
    {
        if (De.Properties.Contains(PropertyName))
        {
            return De.Properties[PropertyName][0].ToString();
        }
        else
        {
            return string.Empty;
        }
    }

//Setting the ADAM Propertie   

public static void SetProperty(DirectoryEntry De, string PropertyName, string PropertyValue)
    {
        if ((PropertyValue != string.Empty) && (PropertyValue != null))
        {
            if (De.Properties.Contains(PropertyName))
            {
                De.Properties[PropertyName][0] = PropertyValue;
            }
            else
            {
                De.Properties[PropertyName].Add(PropertyValue);
            }
        }
    }
 
}

 

In your code behind file access the field like this.

To get the Properties

DirectoryEntry DeUserDetails = new DirectoryEntry();
DeUserDetails = Userdetails.GetUserDetails(strUserName);

txtFirstName.Text = Userdetails.GetProperty(DeUserDetails, “givenName”);

To Set the properties like this

System.DirectoryServices.DirectoryEntry deAddUserDetails = Userdetails.GetUserDetails(txtUserName.Text);
Userdetails.SetProperty(deAddUserDetails, “mail”, txtEmail.Text);

deAddUserDetails.CommitChanges();

 

To update the Password

 Userdetails.SetUserPassword(deAddUserDetails, txtPassword.Text);

Adding a new user to ADAM

 System.DirectoryServices.DirectoryEntry myDE = new DirectoryEntry(Userdetails.ADPath, Userdetails.ADUser, Userdetails.ADPassword, AuthenticationTypes.Secure);
            DirectoryEntries myEntries = myDE.Children;
            System.DirectoryServices.DirectoryEntry deAddUserDetails = myEntries.Add(“CN=” + UserName.Text, “user”);
            Userdetails.SetProperty(deAddUserDetails, “userPrincipalName”, UserName.Text);
            //Userdetails.SetProperty(deAddUserDetails, “userPassword”, txtPassword.Text);
            deAddUserDetails.CommitChanges();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s