Problems with extending s.ds.am.UserPrincipal class

1

I have been trying to extend the s.ds.am.UserPrincipal class in VS 2010 VB app as I require access to AD User properties that are not by default a part of the UserPrincipal. This is a new concept to me and I have found some useful code on here as well but I have run into 2 problems with my extensions that I cannot seem to figure out.

Problem 1: When I attempt to use my UserPrincipalEx to retrieve a user I receive the following error.

ERROR:

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

CODE TO PRODUCE ERROR:

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

Problem 2: When I attempt to create a new user with my UserPrincipalEx I receive the following error. When I trace the PrincipalContext (ctx) into the class it appears to disconnect from the DomainController.

ERROR:

Unknown error (0x80005000)

CODE TO PRODUCE ERROR:

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
vb.net
.net-4.0
userprincipal
extending-classes
principalcontext
asked on Stack Overflow May 6, 2014 by pfitchie • edited May 6, 2014 by Filburt

1 Answer

3

PROBLEM 1

I was able to solve Problem 1 by adding the following to the top of the class after the Imports

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

PROBLEM 2

I have also been banging my head against this for to long and was finally able to resolve Problem 2. The DirectoryOU variable did not contain a correct path the intended AD OU. I was missing a comma and after staring at everything for 30 minutes I finally noticed the issue.

answered on Stack Overflow May 8, 2014 by pfitchie

User contributions licensed under CC BY-SA 3.0