c# - 使用 UserPrincipal 设置 DistinguishedName

标签 c# active-directory userprincipal

当使用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();
    }
}

最佳答案

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

虽然我没有能力测试这个..

关于c# - 使用 UserPrincipal 设置 DistinguishedName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621133/

相关文章:

c# - 创建任务计划程序

c# - 为什么从类派生会导致有关父构造函数的错误?

c# - UserPrincipal.Current 从一天到下一天抛出 COMException

azure - 无法创建具有正确权限的服务主体

c# - 如何在 C# 中的另一个数组内部的结构内部的数组中查找变量的索引

c# - Entity Framework 上下文基础选项的定义

hadoop - Hortonworks CREATE-USER FAILURE密码不符合密码策略要求

active-directory - 如何查看事件目录中已删除的对象

java - 如何使用 Windows 身份验证调用 Active Directory,而不提示输入用户名/密码?