views:

198

answers:

2

I use the following code to create user in Active Directory

DirectoryEntry newUser = null;
            try
            {
                if (!Authenticate()) return null;

                newUser = location.Children.Add("CN=" + userName, "user");
                newUser.Properties["samAccountName"].Value = userName;
                newUser.Properties["cn"].Value = userName;

                newUser.CommitChanges();
                string guid = newUser.Guid.ToString();
                newUser.Invoke("SetPassword", new object[] { password });
                newUser.CommitChanges();
                DomainDirectoryEntry.Close();
                newUser.Close();
                return guid;
            }
            catch
            {
                throw;
            }
            finally
            {
                newUser = null;
            }

If I run this code from asp.net web page it create the user in Active Directory and it created disabled but it throw exception Exception has been thrown by the target of an invocation. and when i get the inner exception of this exception it get me Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDEN I'm sure that the account that i use to access Active Directory is a full admin ..

I don't determined the reason and why it's create the user but it generate error on setting password ... Could any guide me to get the error ?

+1  A: 

I suppose you (App Pool User or Impersonated User) don't have permissions to do that.

By default the user will be NETWORK SERVICE which i am certain doesn't have the permission for that.

How do you know which account you are using to access Active Directory?

Jeroen
but is App pool user affect even i use domain admin user to access active directory from code ...
Space Cracker
A: 

I tried many approaches and by end we found that the problem appear when code run from SharePoint so we put the code in the below code and it run successfully

SPSecurity.RunWithElevatedPrivileges(delegate()
  {
     ......
  }
Space Cracker