无法从C#提交Active Directory中的更改

时间:2015-11-26 08:50:48

标签: c# asp.net active-directory

在以下代码中,我想将用户分配给活动目录中的组。问题是当我提交休息时。我认为它的许可相关错误。在互联网上搜索后,我发现我必须使用

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

但它再次无效。有谁知道如何解决这个问题。 这是我正在使用的代码

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();
                }

            }
        }
    }
}

谢谢。

0 个答案:

没有答案
相关问题