Directory Programming .NET

Active Directory and ADAM programming support for .NET developers
Welcome to Directory Programming .NET Sign in | Join | Help
in Search

Extending the UserPrincipal-Class (Problem with Inheritance)

Last post 08-27-2010, 12:56 AM by daisyfreya123. 2 replies.
Sort Posts: Previous Next
  •  07-22-2010, 4:08 AM 8488

    Extending the UserPrincipal-Class (Problem with Inheritance)

    Hi!

    As there are known problems when retrieving the groups a UserPrincipal belongs to (see http://directoryprogramming.net/forums/1/6649/ShowThread.aspx) I thought I could overload the GetGroups functions (found an example here http://directoryprogramming.net/forums/thread/3581.aspx)

    But it throws me an error when using in the following code:

    insPrincipalContext = New PrincipalContext(ContextType.Domain, "xxx.xxx.xxx.xxx", "dc=xxx,dc=xxxl", "admin", "password")

    Dim insUserPrincipal As New UserPrincipal(insPrincipalContext)
    insUserPrincipal.Name = "*"

    Dim insPrincipalSearcher As New PrincipalSearcher()
    insPrincipalSearcher.QueryFilter = insUserPrincipal
    Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll()

    'Here the error is thrown saying it cannot convert Type 'System.DirectoryServices.AccountManagement.UserPrincipal' to type 'MyExtendedUserPrincipal'

    For Each p As MyExtendedUserPrincipal In results
    Next

    Here is the MyExtendedUserPrincipal-Class:

    Public Class MyExtendedUserPrincipal
    Inherits UserPrincipal

    '''
    ''' test
    '''
    '''
    '''
    Overloads Function GetGroups() As PrincipalSearchResult(Of Principal) 'Use Shadowing instead?
    MsgBox("works")

    End Function

    '''
    ''' Initializes a new instance of the UserPrincipal class by using the specified context
    '''
    ''' The PrincipalContext that specifies the server or domain against which operations are performed
    '''
    Sub New(ByVal context As PrincipalContext)
    MyBase.New(context)
    End Sub


    '''
    ''' Initializes a new instance of the UserPrincipal class by using the specified context,
    ''' SAM account name, password, and enabled value
    '''
    ''' The PrincipalContext that specifies the server or domain against which operations are performed
    ''' The SAM account name for this user principal
    ''' The password for this account
    ''' A Boolean value that specifies whether the account is enabled
    '''
    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

    End Class

    Maybe I'm missing some step. After solving this I will implement the .net 2.0 way to get the groups the user belongs to.

    Thanks for help and regards from Germany

    Jan
  •  08-19-2010, 4:11 PM 8516 in reply to 8488

    Re: Extending the UserPrincipal-Class (Problem with Inheritance)

    Old thread, you may not need an answer any more, but I thought I would throw one out there, anyway, just in case.

    I think your problem is with the line:
    Dim insUserPrincipal As New UserPrincipal(insPrincipalContext)

    Because inUserPrincipal is passed to the QueryFilter, the query returns objects of type UserPrincipal.

    From the MSDN documentation: 
    "The type of the derived class object that is passed to this property determines the type of object that is queried"

    And of course, you can't cast a base class to a descendant type.  Try:
    Dim insUserPrincipal As New MyExtendedUserPrincipal(insPrincipalContext)

    You may also have to implement your own AdvancedSearchFilter method in MyExtendedUserPrincipal... look here for a good example of inheriting from UserPrincipal.

    HTH, or at least clears up an old problem,
    James
  •  08-27-2010, 12:56 AM 8529 in reply to 8516

    Re: Extending the UserPrincipal-Class (Problem with Inheritance)

    I have read your reply and i am sure that this is the best solution.I am also going to use the same and reply you soon with the positive result.

    Trash removal ri
    Junk removal ri
View as RSS news feed in XML