使用UserPrincipalEX创建新用户

本文关键字:用户 新用户 UserPrincipalEX 创建 使用 | 更新日期: 2023-09-27 18:21:10

我正在使用一个扩展来公开UserPrincipal类(通常称为UserPrincipalEX)的更多属性来使用AD。该类在处理"Department"或"Location"等字段时效果很好。如果我试图使用这个类创建一个新帐户,我会收到一个错误,尽管是"服务器不愿意处理请求"。使用本机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

使用UserPrincipalEX创建新用户

我在发布问题后就找到了答案。我不得不将以下行从<DirectoryObjectClass("Person")>修改为<DirectoryObjectClass("User")>