【问题标题】:Setting DistinguishedName using UserPrincipal使用 UserPrincipal 设置 DistinguishedName
【发布时间】:2013-10-27 17:34:46
【问题描述】:

使用DirectoryEntry时,可以设置新用户帐号的CN,但是使用UserPrincipal怎么办呢?该属性是只读的。

// 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();
    }
}

【问题讨论】:

    标签: c# active-directory userprincipal


    【解决方案1】:

    不是您想要的答案,但据我所知,那样做是行不通的...... 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 字符串而不是硬编码)

    虽然我没有机会测试这个..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多