我正在try 创建一个C#控制台应用程序来管理域(而不是文件系统)上的审核权限.我的问题是我无法读取C#中现有的审计规则,我只得到一个空集合,即使我确信域中有审计规则.

我需要管理100多个独立域,所以我想拿出一个程序化的解决方案来帮助管理它们.

我可以在PowerShell中使用以下命令完成此操作,而不会出现问题.

$acl = Get-Acl -Path "DC=mydomain,DC=local" -Audit
$acl.Audit

这将返回5个审计规则的集合.但是,当try 在C#中执行相同的操作时,审计规则不会返回.

string path = "LDAP://mydomain.local/DC=mydomain,DC=local";
DirectoryEntry domain = new DirectoryEntry(path);
               
AuthorizationRuleCollection authorizationRuleList1 = domain.ObjectSecurity.GetAuditRules(true, true, typeof(NTAccount));
AuthorizationRuleCollection authorizationRuleList2 = domain.ObjectSecurity.GetAuditRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));

Console.WriteLine("Authorization Rules 1: " + authorizationRuleList1.Count);
Console.WriteLine("Authorization Rules 2: " + authorizationRuleList2.Count);

运行上面的操作显示0个审计规则.奇怪的是,访问规则没有问题地返回.

我已经try

  1. 解释为DirectoryEntry初始化应用密码
  2. 运行"以管理员身份运行"
  3. 使用LDAP资源管理器判断存在的审核权限

如有任何建议,我们将不胜感激.

更新了使用ActiveDs获取安全描述符的代码.

string path = "LDAP://demo.local/DC=demo,DC=local";
DirectoryEntry domain = new DirectoryEntry(path);
var ntSecurityDescriptor = domain.Properties["ntSecurityDescriptor"];

ActiveDs.ADsSecurityUtility secUtility = new ActiveDs.ADsSecurityUtility();
ActiveDs.IADsSecurityDescriptor sd = (IADsSecurityDescriptor)ntSecurityDescriptor[0];
ActiveDs.IADsAccessControlList aclList =  ActiveDs.IADsAccessControlList)sd.DiscretionaryAcl;

推荐答案

我能够想出一个解决方案来捕获域的审计规则.看起来,单独使用路径加载和DirectoryEntry(如下面的代码所示,不会带回所有属性.

new DirectoryEntry(path)

为了返回自定义属性,可以创建DirectorySearcher,并将其配置为回拉特定属性. 下面的代码块中包含的示例. 在我的情况下,还需要更新SecurityMasks以包括SecurityMasks. Sacl.

string path = "DC=mydomain,DC=local";
DirectorySearcher directorySearcher = new DirectorySearcher($"(distinguishedName={path})");

// Request that the security permissions be returned with the objects.
directorySearcher.PropertiesToLoad.Add("ntSecurityDescriptor");

// Allow section of all Security Descriptors
// This is needed for Audit Rules to be returned.
directorySearcher.SecurityMasks = SecurityMasks.Dacl | SecurityMasks.Owner | SecurityMasks.Sacl;

// Search for Active Directory entries
SearchResultCollection searchResults = directorySearcher.FindAll();

// Convert ntSecurityDesciptor to a RawSecurityDesciptor object
RawSecurityDescriptor rawSecurityDescriptor = new RawSecurityDescriptor((byte[])searchResults[0].Properties["ntSecurityDescriptor"][0], 0);

现在创建了一个原始安全对象,其中包括存储在SystemAcl属性中的审计规则.

rawSecurityDescriptor.SystemAcl

此对象将允许查看该对象的审核规则.

希望这对其他人有帮助.

Csharp相关问答推荐

使用GeneratedComInterfaceProperty的.NET 8 COM类对于VB 6/SYS或ALEViewer不可见

使用LayoutKind在C#中嵌套 struct .显式

将XPS转换为PDF C#

如何使用C#Interop EXCEL创建度量衡

使用Audit.EntityFramework,我如何将外键的值设置为相关实体上的属性?

Thad.Sept()vs Task.Delay().Wait()

NET8 MAUI并部署到真实设备上进行测试

C#带主体的主构造函数?

DbContext-传递自定义配置选项

BFF到具有AAD/Entra ID B2C的内部API(.NET/ASP.NET核心/标识.Web)

VS 2022与VS 2019:如何/为什么创建额外的任务?

.NET8->;并发词典总是比普通词典快...怎么回事?[包含基准结果和代码]

如何在.NET MAUI中最大化GraphicsView的大小?

如何使用实体框架核心对字符串_agg使用强制转换为varchar(Max)

try 创建一个C#程序,该程序使用自动实现的属性、覆盖ToString()并使用子类

删除MudRadio时,MudRadioGroup未 Select 正确的MudRadio

如何从原始图像到新创建的图像获得相同的特定 colored颜色 ,并且具有相同的 colored颜色 量和相同的宽度和高度?

C#定时器回调对象上下文?

使用C#代码和SQL SERVER中的相同证书签名会产生不同的结果

初始化具有EntityFrameworkCore和不同架构的数据库时引发System.MissingMethodExcept