2012-04-24 137 views
0

我試圖通過使用UserPrincipal更改Active Directory中的用戶帳戶屬性。使用UserPrincipal更改AD用戶帳戶屬性

我讀過,我們必須使用具有對Active Directory的寫入訪問權限而不是當前登錄用戶的特殊帳戶。所以,我創建了特殊課程,通過使用特殊帳戶來模仿。但我仍然有

System.UnauthorizedAccessException: General access denied error

在user.Save(CTX);線。

System.Security.Principal.WindowsImpersonationContext newUser = clsImpersonate.ImpersonateUser("ADUser", "ADPassword"); 

      if (newUser != null) 
      { 
       PrincipalContext ctx = blAD.GetAdminPrincipalContext(); 
       UserPrincipal user = blAD.GetUserPrincipal(this.SAMAccount); 
       user.Enabled = false; 
       user.Save(ctx); 
       newUser.Undo(); 
      } 

我該如何達到這個要求?謝謝。

回答

0

什麼權限已委派給您的特殊用戶?它需要能夠對有問題的用戶編寫userAccountControl

0

我不會首先冒充帳戶!首先通過廣告傳遞價值來獲得訪問權限。

對於真正的問題,看看錯誤:

  1. 獲取principalContect。
  2. 獲取userprincipal。
  3. 做你想做的事。
  4. 保存它,爲什麼你使用撤消?刪除撤銷()。
0

要以其他用戶的身份訪問原則,請使用用戶的憑證定義您的PrincipalContext,並在獲取UserPrincipal時使用該PrincipalContext。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "domain.tld", "ADUser", "ADPassword"); 
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, this.SAMAccount); 
    if (user != null) 
    { 
     user.Enabled = false; 
     user.Save(); 
    } 

如果你仍然得到UnauthorizedAccess例外,因爲您指定的帳戶沒有訪問寫在Active Directory/LDS用戶對象的userAccountControl屬性是可能的。

相關問題