Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems implementing UserPrincipal extension class in VB.NET

Status
Not open for further replies.

frumpus

Programmer
Aug 1, 2005
113
US
I am working with the DirectoryServices.AccountManagement tools in VB.NET, specifically, finding a UserPrincipal, but I need to retrieve the Department field which that class does not offer. So I have implemented this extension class, which I found here. According to that post it was generated for VB from C# and was apparently not really tested.

Code:
Imports System.DirectoryServices.AccountManagement

Public Class UserPrincipalEx
    <DirectoryRdnPrefix("CN")> _
    <DirectoryObjectClass("Person")> _
    Inherits UserPrincipal
    ' Implement the constructor using the base class constructor. 
    Public Sub New(ByVal context As PrincipalContext)
        MyBase.New(context)
    End Sub

    ' Implement the constructor with initialization parameters.    
    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

    ' 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(ByVal value As String)
            ExtensionSet("department", 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

This all compiles fine, but when I modified my code to use the extension class instead of the standard UserPrincipal, it doesn't work. Here is the code that is attempting to use the extension:

Code:
Dim insPrincipalContext As New PrincipalContext(ContextType.Domain, Environment.UserDomainName, "DC=mydomain,DC=com")
Dim insUserPrincipal As New UserPrincipalEx(insPrincipalContext)
insUserPrincipal.Description = empID
Dim insPrincipalSearcher As New PrincipalSearcher()
Dim currentADUser As UserPrincipalEx
insPrincipalSearcher.QueryFilter = insUserPrincipal
Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll

The last line above generates the error:

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

I'm pretty new to vb.net and I'm not sure what is causing the issue. Any advice on how to resolve it would be greatly appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top