【问题标题】:Create a new user using UserPrincipalEX使用 UserPrincipalEX 创建新用户
【发布时间】:2014-09-08 19:58:28
【问题描述】:

我正在使用扩展来公开 UserPrincipal 类(通常称为 UserPrincipalEX)的更多属性以与 AD 一起使用。该类在处理“部门”或“位置”等字段时效果很好。如果我尝试使用此类创建一个新帐户,我会收到一个错误,尽管“服务器不愿意处理请求”。使用本机 UserPrincipal 类,我可以毫无问题地创建帐户。

这里是 UserPrincipalEX 代码的核心:

Imports System.DirectoryServices.AccountManagement

<DirectoryRdnPrefix("CN")> _
<DirectoryObjectClass("Person")> _
Public Class UserPrincipalEx
Inherits UserPrincipal

Public Sub New(context As PrincipalContext)
    MyBase.New(context)
End Sub


Public Sub New(context As PrincipalContext, samAccountName As String, password As String, enabled As Boolean)
    MyBase.New(context, samAccountName, password, enabled)
End Sub

Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityValue As String) As UserPrincipalEx
    Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityValue), UserPrincipalEx)
End Function

Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityType As IdentityType, identityValue As String) As UserPrincipalEx
    Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityType, identityValue), UserPrincipalEx)
End Function

<DirectoryProperty("Company")> _
Public Property Company() As String
    Get
        If ExtensionGet("company").Length <> 1 Then
            Return String.Empty
        End If

        Return DirectCast(ExtensionGet("company")(0), String)
    End Get
    Set(value As String)
        ExtensionSet("company", value)
    End Set

End Property

End Class

【问题讨论】:

    标签: c# vb.net active-directory


    【解决方案1】:

    我在发布问题后立即找到了答案。我不得不将以下行从 &lt;DirectoryObjectClass("Person")&gt; 修改为 &lt;DirectoryObjectClass("User")&gt;

    【讨论】: