[英]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.