延伸s.ds.am.UserPrincipal类问题(Problems with extending

2019-10-20 01:56发布

我一直在努力,因为我需要访问不在默认UserPrincipal的一部分AD用户属性来扩展在VS 2010 VB应用程序的s.ds.am.UserPrincipal类。 这是一个全新的概念对我来说,我发现这里上一些有用的代码很好,但我有我的扩展,我似乎无法找出碰到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)入类似乎从网域控制器断开连接。

错误:

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

Answer 1:

问题1

我能够通过进口后添加以下的类的顶部,以解决问题1

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

问题2

我也一直在敲我的头这长和终于能解决问题2. DirectoryOU变量不包含正确的路径预期的AD OU。 我错过了一个逗号,并在一切盯着30分钟后,我终于注意到了这个问题。



文章来源: Problems with extending s.ds.am.UserPrincipal class