我一直在尝试扩展VS 2010 VB应用程序中的s.ds.am.UserPrincipal类,因为我需要访问默认情况下不属于UserPrincipal的AD用户属性。这对我来说是一个新概念,我在这里也找到了一些有用的代码,但是我的扩展遇到了两个问题,我似乎无法弄明白。
问题1:当我尝试使用UserPrincipalEx检索用户时,收到以下错误。
ERROR:
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断开连接。
ERROR:
Unknown error (0x80005000)
产生错误的代码:
Dim ctx As New PrincipalContext(ContextType.Domain, DomainController, DirectoryOU)
Dim NewADUser As New UserPrincipalEx(ctx, newsamAccountName, newPassword, True)
CLASS:
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
答案 0 :(得分:3)
问题1
我能够通过在Imports
之后将以下内容添加到类的顶部来解决问题1<DirectoryRdnPrefix("CN")> _
<DirectoryObjectClass("user")> _
问题2
我一直在坚持不懈地对付这个问题,并且最终能够解决问题2. DirectoryOU变量没有包含预期AD OU的正确路径。我错过了一个逗号,在盯着所有东西30分钟后,我终于发现了这个问题。