1

我正在尝试添加部门和标题等字段。

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

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++ 中的,并且不知道如何去做。

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

4

1 回答 1

1

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

要扩展这个UserPrincipal类,你不需要太多——这样的东西就足够了(我最初是用 C# 编写的,只是在网上将它转换为 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 中与用户和组一起玩变得非常容易!

于 2013-02-21T21:27:52.733 回答