c# 活动目录临时组成员身份?

c# active directory temporary groupmembership?

有什么方法可以在准确的时间将用户添加到组中,然后自动将用户从组中删除?
例如。: CN=testuser1CN=testgroup1
现在我想将 CN=testuser1 添加到 CN=testgroup1 1 天。
在那 1 天之后,用户应该离开(不应再是该组的成员)CN=testgroup1 自动
System.DirectoryServices.AccountManagement;System.DirectoryServices; 是否可行,或者是否有除 Powershell 脚本以外的其他解决方案?

提示:我不想要带有 powershell 脚本或类似东西的解决方案。它应该在我的 c# 程序中完成。我有一个 windows 表单,其中有 3 个文本框:

当我按下按钮时 "Add user temporarily to group" 用户应该被添加到该组的特定时间,我可以在该持续时间文本框中输入该时间。

提前致谢!

是的,这是可能的。它要求您拥有一个 Windows Server 2016 林,并且您启用了特权访问管理可选功能。

有了这个之后,您就可以为链接值(例如组成员身份)指定 TTL。此博客 https://www.dsinternals.com/en/how-the-active-directory-expiring-links-feature-really-works/ 向您展示了如何执行此操作。我不知道您是否可以通过 ADSI (System.DirectoryServices) 提供语法,或者您是否需要回退到使用 System.DirectoryServices.Protocols.

的直接 LDAP 调用

Brian 回答了这个问题 - 这是一个 C# 示例,说明我们如何按照对他的回答的评论的要求完成此操作。

using (GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName))
{
    TimeSpan span = accessUntil.Subtract(dtNow.Value);
    DirectoryEntry groupDirectoryEntry = (DirectoryEntry)group.GetUnderlyingObject();
    groupDirectoryEntry.Properties["member"].Add("<TTL=" + ((int)span.TotalSeconds).ToString() + "," + user.DistinguishedName + ">");
    groupDirectoryEntry.CommitChanges();
    groupDirectoryEntry.Close();
}

请注意,如果成员已经存在 TTL,则“添加”将替换当前的 TTL。