3

我正在尝试让我自己的静态类与 AD 一起使用。我写了一个静态方法:

    public static void AddReadingAceForGroup(DirectoryEntry dirEntry, string groupName)
    {
        dirEntry.RefreshCache();
        DirectoryEntry root = new DirectoryEntry("LDAP://192.168.1.1/       dc=mydomain,dc=ru");
        using (DirectorySearcher ds = new DirectorySearcher(root, "CN="+groupName))
        {
            SearchResult sr = ds.FindOne();
            root = sr.GetDirectoryEntry();
        }
        try
        {
            ActiveDirectoryAccessRule accessRule =
                new ActiveDirectoryAccessRule(root.ObjectSecurity.GetGroup(typeof(SecurityIdentifier)),
                                              ActiveDirectoryRights.GenericRead, AccessControlType.Allow);
            dirEntry.ObjectSecurity.AddAccessRule(accessRule);
            dirEntry.CommitChanges();
        }
        catch(Exception e)
        {
        }
    }

在使用此功能之前,我使用远程凭据模拟用户,然后代码正常工作,但没有结果。删除 ACE 的类似功能可以正常工作。

4

1 回答 1

0

最终的工作代码是:

public static SecurityIdentifier GetGroupSid(string groupName, string domainControllerIp)
{
    SecurityIdentifier sid = null;
    using (PrincipalContext dcx = new PrincipalContext(ContextType.Domain, domainControllerIp))
    {
        GroupPrincipal group = GroupPrincipal.FindByIdentity(dcx, groupName);
        if (group != null)
        {
            sid = group.Sid;
            group.Dispose();
        }
    }
    return sid;
}
public static void AddDaclsAceForGroup(DirectoryEntry dirEntry, string groupName, string ip)
{
    SecurityIdentifier sid = GetGroupSid(groupName,ip);
    try
    {
        ActiveDirectoryAccessRule accessRule =
            new ActiveDirectoryAccessRule(sid,ActiveDirectoryRights.GenericRead, AccessControlType.Allow);
        dirEntry.ObjectSecurity.AddAccessRule(accessRule);
        dirEntry.CommitChanges();
    }
    catch(Exception e)
    {
    }
}

我只是在组 SID 上有错误。代码运行完美,但不是我所期望的。对不起我的英语不好。

于 2012-03-19T10:21:23.343 回答