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.