首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用DistinguishedName设置UserPrincipal

使用DistinguishedName设置UserPrincipal
EN

Stack Overflow用户
提问于 2013-10-27 17:34:46
回答 1查看 2.8K关注 0票数 3

当使用DirectoryEntry时,可以设置新用户帐户的CN,但是如何使用UserPrincipal进行设置呢?该属性是只读的。

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
// 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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 13:54:47

不是你想要的答案,但据我所知,那是不可能的.CN是用户原则类中的“受保护”类,因为其他地方太多地依赖于稳定的信息。

我不知道为什么会把事情搞混,但你可以试试这个:

代码语言:javascript
复制
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字符串,而不是硬编码)

不过,我没有这个可能性来检验这个。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19621133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档