Imports System.DirectoryServices.AccountManagement

<DirectoryRdnPrefix("CN")>
<DirectoryObjectClass("Person")>
Public Class UserPrincipalEx
    Inherits UserPrincipal

    Public Sub New(Context As PrincipalContext)
        MyBase.New(Context)
    End Sub

    Public Sub New(Context As PrincipalContext, samAccountName As String, Password As String, Enabled As Boolean)
        MyBase.New(Context, samAccountName, Password, Enabled)
    End Sub

    Public Overloads Shared Function FindByIdentity(ByVal Context As PrincipalContext, ByVal IdentityValue As String) As UserPrincipalEx
        Return CType(FindByIdentityWithType(Context, GetType(UserPrincipalEx), IdentityValue), UserPrincipalEx)
    End Function

    Public Overloads Shared Function FindByIdentity(ByVal Context As PrincipalContext, ByVal IdentityType As IdentityType, ByVal IdentityValue As String) As UserPrincipalEx
        Return CType(FindByIdentityWithType(Context, GetType(UserPrincipalEx), IdentityType, IdentityValue), UserPrincipalEx)
    End Function

    Public Function GetAttributeValue(AttributeName As String) As String
        Return TryGetAttribute(AttributeName)
    End Function

    Private Function TryGetAttribute(AttributeName As String) As String
        Dim oAttribute = ExtensionGet(AttributeName)

        If oAttribute.Length <> 1 Then
            Return String.Empty
        End If
        Return CStr(oAttribute(0))
    End Function
End Class
