【问题标题】:VB.net creating new AD user account using UserPrincipalEx?VB.net 使用 UserPrincipalEx 创建新的 AD 用户帐户?
【发布时间】:2023-07-29 03:13:01
【问题描述】:

我在尝试添加部门和职位等字段时遇到了麻烦。

我正在使用它来创建一个用户帐户:

Dim ctx As New PrincipalContext(ContextType.Domain, "domain.name.pvt",  "OU=Users,DC=global,DC=pvt")

Dim usr As UserPrincipal = New UserPrincipal(ctx) 

我创建帐户没有问题,但无法添加DepartmentTitle 之类的简单内容。我阅读了有关使用扩展的信息,但它是在 C++ 中的,并且不知道如何去做。

任何帮助都会很棒!!!谢谢!

【问题讨论】:

    标签: vb.net active-directory directoryservices


    【解决方案1】:

    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    要扩展 UserPrincipal 类,您不需要太多 - 这样就足够了(我最初是用 C# 编写的,只是在 'net 上将它转换为 VB.NET - 我希望没有VB.NET 代码的问题!)

    Imports System.DirectoryServices.AccountManagement
    
    Namespace ADExtended
        <DirectoryRdnPrefix("CN")> _
        <DirectoryObjectClass("Person")> _
        Public Class UserPrincipalEx
            Inherits UserPrincipal
            ' Inplement the constructor using the base class constructor. 
            Public Sub New(context As PrincipalContext)
                MyBase.New(context)
            End Sub
    
            ' Implement the constructor with initialization parameters.    
            Public Sub New(context As PrincipalContext, samAccountName As String, password As String, enabled As Boolean)
                MyBase.New(context, samAccountName, password, enabled)
            End Sub
    
            ' Create the "Department" property.    
            <DirectoryProperty("department")> _
            Public Property Department() As String
                Get
                    If ExtensionGet("department").Length <> 1 Then
                        Return String.Empty
                    End If
    
                    Return DirectCast(ExtensionGet("department")(0), String)
                End Get
                Set
                    ExtensionSet("department", value)
                End Set
            End Property
    
            ' Create the "Title" property.    
            <DirectoryProperty("title")> _
            Public Property Title() As String
                Get
                    If ExtensionGet("title").Length <> 1 Then
                        Return String.Empty
                    End If
    
                    Return DirectCast(ExtensionGet("title")(0), String)
                End Get
                Set
                    ExtensionSet("title", value)
                End Set
            End Property
    
            ' Implement the overloaded search method FindByIdentity.
            Public Shared Shadows Function FindByIdentity(context As PrincipalContext, identityValue As String) As UserPrincipalEx
                Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityValue), UserPrincipalEx)
            End Function
    
            ' Implement the overloaded search method FindByIdentity. 
            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
        End Class
    End Namespace
    

    现在,您只需使用 UserPrincipalEx 类:

    Dim ctx As New PrincipalContext(ContextType.Domain, "domain.name.pvt",  "OU=Users,DC=global,DC=pvt")
    
    Dim usr As UserPrincipalEx = New UserPrincipalEx(ctx) 
    usr.Title = "......."
    usr.Department = "......."
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

    • 谢谢!我会试试。我是任何命名空间的新手。我会努力解决这个问题,非常感谢!!
    • 谢谢,我没有让它工作 :( 嘘 :( 当我输入代码时,它在 Public Sub New() 中说太多参数。这就是我将鼠标悬停在 (ctx) 上时的消息
    • 您是否尝试过使用 UserPrincipalEx 类创建新用户?我收到错误消息说“服务器不愿意处理请求”。其他功能似乎很好用,例如更新部门或员工 ID。