HI I have devloped an application for Active Directory to add User but I am unable to setPassword for the created User.

Jul 29 2008 2:58 AM
Plese can any one tell me how to setpassword for user in Active Directory, the User is created and set the password.

i have attached the code please chaeck and give me slolution.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;

namespace CreateUser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void CreateUserAccountAd()
        {
            string strUser = "CN=Man";
            string strPassword = "satyam123$";
            string strpath = "LDAP://csc.com/OU=csc,DC=csc,DC=com";
            string strUserId = "adminstrator";
            DirectoryEntry de = new DirectoryEntry(strpath);
            DirectoryEntries child = de.Children;
            DirectoryEntry deuser = child.Add(strUser, "user");
            deuser.Properties["sAMAccountName"].Add(strUserId);
            deuser.CommitChanges();
            //SetPassword(deuser, strPassword);
            //Enable(deuser);
        }
        private void CreateUser_Click(object sender, EventArgs e)
        {
           
            //MessageBox.Show(textFirstName.Text);
            //CreateUserAccountAd();
            //MessageBox.Show(textUserName.Text);
            CreateUserAccount();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void CreateUserAccount()
        {
            try
            {
                string oGUID = string.Empty;
                //string connectionPrefix = "LDAP://csc.com/OU=csc,DC=csc,DC=com";
                string connectionPrefix = "LDAP://csc.com/CN=Users,DC=csc,DC=com";
                DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix,"administrator","satyam123$");
                DirectoryEntry newUser = dirEntry.Children.Add("CN=" + "Guru_Managoli", "user");
                newUser.Properties["samAccountName"].Value = "Guru_Managoli";
                ///newUser.Properties["Password"].Value = "satyam123$";
                newUser.CommitChanges();
                oGUID = newUser.Guid.ToString();
                Enable();
                //newUser.Invoke("SetPassword", new object[] { "satyam123$" });
                newUser.Password = "satyam123$";
                newUser.CommitChanges();
                dirEntry.Close();
                newUser.Close();
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //DoSomethingwith --> E.Message.ToString();

            }
        }
        public void Enable()
        {
            try
            {
                DirectoryEntry user = new DirectoryEntry("LDAP://csc.com/CN=ricky_patnaik,CN=Users,DC=csc,DC=com", "administrator", "satyam123$");
                int val = (int)user.Properties["userAccountControl"].Value;
                user.Properties["userAccountControl"].Value = val & ~0x2;
                //ADS_UF_NORMAL_ACCOUNT;

                user.CommitChanges();
                user.Close();
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                //DoSomethingWith --> E.Message.ToString();

            }
        }

    }


}

Answers (1)