活动目录。使用 DACL

发布于 2025-01-01 00:47:01 字数 994 浏览 1 评论 0原文

我正在尝试创建自己的静态类来与 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 的类似功能效果很好。

I'm trying to make my own static class to work with AD.
I wrote a static method:

    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)
        {
        }
    }

Before using this function I do impersonate user with remote credentials, then code works without exceptions, but have no result. The similar function which removes ACE works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2025-01-08 00:47:01

最终的工作代码是:

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 错误。该代码工作完美,但没有达到我的预期。
抱歉我的英语不好。

The final working code is:

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)
    {
    }
}

I just had error with group SID. The code works perfect, but do not that what I'm expecting for.
Sorry my bad english.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文