1

我一直在尝试扩展 VS 2010 VB 应用程序中的 s.ds.am.UserPrincipal 类,因为我需要访问默认情况下不属于 UserPrincipal 的 AD 用户属性。这对我来说是一个新概念,我也在这里找到了一些有用的代码,但是我的扩展遇到了 2 个我似乎无法弄清楚的问题。

问题 1:当我尝试使用我的 UserPrincipalEx 检索用户时,我收到以下错误。

错误:

Principal objects of type MyApp.UserPrincipalEx can not be used in a query against this store

产生错误的代码:

Dim ctx As New PrincipalContext(ContextType.Domain, DomainController)
Dim user As UserPrincipalEx = UserPrincipalEx.FindByIdentity(ctx, samAccountName)

问题 2:当我尝试使用 UserPrincipalEx 创建新用户时,我收到以下错误。当我将 PrincipalContext (ctx) 跟踪到该类时,它似乎与 DomainController 断开连接。

错误:

Unknown error (0x80005000)

产生错误的代码:

Dim ctx As New PrincipalContext(ContextType.Domain, DomainController, DirectoryOU)
Dim NewADUser As New UserPrincipalEx(ctx, newsamAccountName, newPassword, True)

班级:

Imports System.DirectoryServices.AccountManagement
Public Class UserPrincipalEx
Inherits UserPrincipal

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

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

<DirectoryProperty("Title")> _
Public Property Title() As String
    Get
        If ExtensionGet("title").Length <> 1 Then
            Return Nothing
        End If


        Return DirectCast(ExtensionGet("title")(0), String)
    End Get
    Set(ByVal value As String)
        Me.ExtensionSet("title", value)
    End Set
End Property
<DirectoryProperty("physicalDeliveryOfficeName")> _
Public Property physicalDeliveryOfficeName() As String
    Get
        If ExtensionGet("physicalDeliveryOfficeName").Length <> 1 Then
            Return Nothing
        End If


        Return DirectCast(ExtensionGet("physicalDeliveryOfficeName")(0), String)
    End Get
    Set(ByVal value As String)
        Me.ExtensionSet("physicalDeliveryOfficeName", value)
    End Set
End Property
<DirectoryProperty("Company")> _
Public Property Company() As String
    Get
        If ExtensionGet("company").Length <> 1 Then
            Return Nothing
        End If


        Return DirectCast(ExtensionGet("company")(0), String)
    End Get
    Set(ByVal value As String)
        Me.ExtensionSet("company", value)
    End Set
End Property
' Implement the overloaded search method FindByIdentity. 
Public Shared Shadows Function FindByIdentity(ByVal context As PrincipalContext, ByVal 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(ByVal context As PrincipalContext, ByVal identityType As IdentityType, ByVal identityValue As String) As UserPrincipalEx
    Return DirectCast(FindByIdentityWithType(context, GetType(UserPrincipalEx), identityType, identityValue), UserPrincipalEx)
End Function
End Class
4

1 回答 1

4

问题 1

我能够通过在 Imports 之后将以下内容添加到类的顶部来解决问题 1

<DirectoryRdnPrefix("CN")> _
<DirectoryObjectClass("user")> _

问题 2

我也一直在努力解决这个问题,终于能够解决问题 2。 DirectoryOU 变量不包含预期 AD OU 的正确路径。我错过了一个逗号,在盯着所有东西看了 30 分钟后,我终于注意到了这个问题。

于 2014-05-08T19:41:34.167 回答