0

In the following code I want to assign user to a group in active directory. The problem is when I commit it breaks. I think its permission related error. After searching on internet I found that I have to use

using (HostingEnvironment.Impersonate())
{ ... }

but again its not working. Does anyone know how to fix this issue. Here is the code I am using

using (HostingEnvironment.Impersonate())
{
    using (var pc = new PrincipalContext(ContextType.Domain, "192.168.1.100", "username", "password"))
    {
        using (var up = new UserPrincipal(pc))
        {
            //up.Name = txtFirstName.Text + " " + txtLastName.Text;
            //up.Save();
        }

        using (var searcher = new PrincipalSearcher(new UserPrincipal(pc)))
        {
            foreach (var result in searcher.FindAll())
            {
                DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                if (ASPxGridView1.GetRowValues(ASPxGridView1.FocusedRowIndex, "Корисничко име").ToString() == de.Properties["samAccountName"].Value.ToString())
                {
                    try { de.Properties["member"].Add(edGrupi.SelectedItem.Text.ToString()); }
                    catch { }
                    de.CommitChanges();
                }

            }
        }
    }
}

Thank you.

Barlet
  • 545
  • 2
  • 8
  • 26
  • What exception? `try { de.CommitChanges(); } catch(Exception ex) { }` – BWA Nov 26 '15 at 08:56
  • this is the exception I get: System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operations error occurred. at System.DirectoryServices.DirectoryEntry.CommitChanges() at AdminPanel.DodajKPoGrp.AssignUserToGroup() – Barlet Nov 26 '15 at 09:08
  • I remember getting a 0x80072020 when I ran a .net 4.0 application on a .net 3.5. unfortunately the same error is given for lots of problems but its always worth checking. – Nick Otten Nov 26 '15 at 09:15
  • Check this, similar problem: [link](http://stackoverflow.com/questions/17103066/directoryservicescomexception-0x80072020-when-using-active-directory-from-asp) or [link](http://stackoverflow.com/questions/13688031/system-directoryservices-directoryservicescomexception-an-operations-error-occu) – BWA Nov 26 '15 at 09:51
  • What is `edGrupi.SelectedItem.Text.ToString()`? – Brian Desmond Nov 27 '15 at 21:27
  • @BrianDesmond it's a dropdown list (ComboBox) – Barlet Nov 28 '15 at 19:24
  • Right but what is the format of the value? – Brian Desmond Nov 30 '15 at 21:35

0 Answers0