Imports System.DirectoryServices
Imports System.Security.Principal
Imports System.DirectoryServices.AccountManagement

Partial Class Common
	Inherits System.Web.UI.MasterPage
    Dim tmpUserName As String
    Dim LenOfUsername As Integer
    Dim username1 As String

    Dim userFirst As String
    Dim UserMiddleName As String
    Dim userLast As String

    '
    ' private data members
    '
    Private _titlebarDate
    Private _userID

    '
    ' data member access methods
    '
    Public ReadOnly Property titleBarDate() As String
        Get
            Return _titlebarDate
        End Get
    End Property

    Public ReadOnly Property userID() As String
        Get
            Return _userID
        End Get
    End Property

    '
    ' start of class methods
    '
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        tmpUserName = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name
        LenOfUsername = tmpUserName.Length
        Session.Item("empUsrName") = Mid(tmpUserName, 7, (LenOfUsername - 6))


        Dim tmpDate As DateTime

        tmpDate = DateTime.Now
        _titlebarDate = tmpDate.ToLongDateString()

        _userID = getUserID()

        TitlebarDateFld.Text = _titlebarDate
        UserIDFld.Text = _userID
        Session.Item("iUserID") = _userID
        lblAppMsg.Text = System.Web.HttpContext.Current.Session(“Oops”).ToString()
        ' Clear session variable in case of subsequent user tries
        Session.Item("Oops") = ""

    End Sub

    Function getUserID()
        Dim tmpVar As String
        tmpVar = Page.User.Identity.Name
        Return tmpVar
    End Function

    'Function getUserID()
    '    Try
    '        Dim tmpVar As String
    '        'Page.User.Identity.Name

    '        Dim domainctx As New PrincipalContext(ContextType.Domain, "DC=V17", "DC=med,DC=va,DC=gov")

    '        Dim userQuery As UserPrincipal = UserPrincipal.FindByIdentity(domainctx, Session.Item("empUsrName"))

    '        If userQuery IsNot Nothing Then
    '            username1 = userQuery.Name
    '            userFirst = userQuery.GivenName
    '            UserMiddleName = userQuery.MiddleName
    '            userLast = userQuery.Surname

    '            tmpVar = userFirst + " " + UserMiddleName + " " + userLast

    '            Return tmpVar
    '        Else
    '            '  tmpVar = userFirst + " " + UserMiddleName + " " + userLast

    '            Return Session.Item("empUsrName")
    '        End If
    '    Catch
    '        Return Session.Item("empUsrName")
    '        ' MsgBox("Server is not operational")
    '    End Try
    'End Function
End Class

