2013-10-27 20 views
2

使用DirectoryEntry時,可以設置新用戶帳戶的CN,但是如何使用UserPrincipal?該酒店是隻讀的。使用UserPrincipal設置DistinguishedName

// From : http://msdn.microsoft.com/en-us/magazine/cc135979.aspx 
DirectoryEntry container = new DirectoryEntry("LDAP://ou=TechWriters,dc=fabrikam,dc=com"); 

// create a user directory entry in the container 
DirectoryEntry newUser = container.Children.Add("cn=user1Acct", "user"); 
// add the samAccountName mandatory attribute 
newUser.Properties["sAMAccountName"].Value = "User1Acct"; 
// save to the directory 
newUser.CommitChanges(); 

但使用UserPrincipal:

// For the example 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "ou=TechWriters,dc=fabrikam,dc=com") 
{ 
    using (UserPrincipal user = new UserPrincipal(ctx, "User1Acct", "pwd", true)) 
    { 
     // I would like to do : 
     user.DistinguishedName = "user1Acct"; 
     // 
     user.Save(); 
    } 
} 

回答

2

不是你想要的答案,但據我所知它的不可行這樣...的CN是「保護」奧姆的userprinciple類,因爲太多其他地方依賴於這是穩定的信息。

我不知道爲什麼人們會混爲一談,但你可以試試這個:

using (var ctx = new PrincipalContext(ContextType.Domain, null, "ou=TechWriters,dc=fabrikam,dc=com")) 
     { 
      using (var user = new UserPrincipal(ctx, "User1Acct", "pwd", true)) 
      { 
       user.Save(); 
      } 

      using (var entry = new DirectoryEntry("LDAP://cn=User1Acct;ou=TechWriters,dc=fabrikam,dc=com",null,null,AuthenticationTypes.Secure)) 
      { 
       entry.Rename("cn=user1Acct"); 
      } 
     } 

(也許越來越從userPrinciple而不是硬編碼的LDAP字符串)

我沒有可能性測試這雖然..

相關問題