![](/img/trans.png)
[英]Accessing Active Directory using current user context with impersonation switched on
[英]Add users to active directory through user impersonation
我有一个 Windows 应用程序,它允许应用程序用户将用户添加/删除到 Active Directory 组中。 应用程序用户使用他们的 Windows 凭据登录到应用程序。 但是,所有个人用户都无权在 AD 组中添加/删除用户。 我想在内部模拟一个对 AD 组具有修改权限的用户。 我正在使用下面的代码,我从 SO 的不同答案中获取它。 不确定,如果我用错了。 但我有一个例外。
使用此库进行模拟: https : //www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User
using (new Impersonator("username", "domain", "passowrd"))
{
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find your user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "user");
if (user != null)
{
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "Group Name");
// if found....
if (group != null)
{
// add user to group
group.Members.Add(user);
group.Save();
}
}
}
}
如果我使用具有适当权限的用户登录,则可以从 AD 添加/删除用户。 但不是通过冒充。
您不需要模拟来使用不同的凭据连接到 AD。 只需使用接受用户名和密码的PrincipalContext
构造函数:
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "username", "password"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.