2011-03-24 102 views
2

我正在嘗試配置一些用戶更新他們在活動目錄(AD)中的電子郵件地址。我試圖用MembershipUser類來實現它。但獲得'一般訪問被拒絕錯誤'。這裏是我的代碼:AD使用MembershipUser更新電子郵件地址 - 訪問被拒絕

 string userName = "sathish"; 

     System.Web.Security.MembershipUser userDetails = System.Web.Security.Membership.GetUser(userName); 
     if (userDetails != null) 
     { 
      userDetails.Email = "[email protected]"; 
      System.Web.Security.Membership.UpdateUser(userDetails); // getting access denied error here 
     } 

我的問題是,

  1. 我需要適當previleges更新電子郵件地址AD?

  2. 我們是否有任何屬性來驗證我目前的訪問級別?

  3. 是否可以以編程方式模擬特權來更新電子郵件地址?

回答

1

如果您使用的是.NET 3.5或更高版本,則應該檢查System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。在這裏閱讀全部內容:

Managing Directory Security Principals in the .NET Framework 3.5

基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:

// set up domain context for your current, default domain 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find user by name 
string userName = "sathish"; 
UserPrincipal user = UserPrincipal.FindByIdentity(userName); 

// if user is found - update it's e-mail address and save 
if(user != null) 
{ 
    user.EmailAddress = "[email protected]"; 
    user.Save(); 
} 

新S.DS.AM使它真的很容易與在AD的用戶和羣體玩:

+1

感謝您的響應和鏈接!事實上,它幫助我接近這個問題。一旦我確定了這一點,我會迴應。 正如你所說的,Account Maangement提供了很多更多的服務,我正在用這個AccountManagement服務重寫我的ActiveDirectory提供程序。 – 2011-03-26 10:10:31