Namespace Training2.UI.Controls.SignIn_Control
#Region "Section 1: Place your customizations here."
    Public Class SignInControl
        Inherits BaseSignInControl

    End Class
#End Region

#Region "Section 2: Do not modify this section."
    Public Class BaseSignInControl
        Inherits System.Web.UI.Control

#Region " Constants "
        Const INVALID_USER_INFO As Integer = -2147467259
#End Region

#Region " Protected Properties "

        Public ReadOnly Property ERR_INVALID_LOGIN_INFO As String
            Get
                Return DirectCast(Me.Page, BaseApplicationPage).GetResourceValue("Err:InvalidLoginInfo", "Training2")
            End Get
            
        End Property

        Public ReadOnly Property ERR_INTERNAL_ERROR As String
            Get
                Return DirectCast(Me.Page, BaseApplicationPage).GetResourceValue("Err:InternalErrorLogin", "Training2")
            End Get
            
        End Property

        Public ReadOnly Property LOGIN_MSG As String
            Get
                Return DirectCast(Me.Page, BaseApplicationPage).GetResourceValue("Txt:LoginMsg", "Training2")
            End Get
            
        End Property

        Public ReadOnly Property LOGIN_MSG_SESSION_INVALID As String
            Get
                Return DirectCast(Me.Page, BaseApplicationPage).GetResourceValue("Txt:LoginMsgSessionInvalid", "Training2")
            End Get
            
        End Property

#End Region

#Region " Events "
        Public Event LoginSucceeded(ByVal sender As Object, ByVal e As System.EventArgs)
        Public Event LoginFailed(ByVal sender As Object, ByVal e As System.EventArgs)
#End Region

#Region " Public Properties "
        'URL to redirect to when login is successful
        Protected _successURL As String
        Public Property SuccessURL() As String
            Get
                Return Me._successURL
            End Get
            Set(ByVal Value As String)
                Me._successURL = Value
            End Set
        End Property

        'URL parameter name for SuccessURL
        Protected _successURLParm As String
        Public Property SuccessURLParam() As String
            Get
                Return Me._successURLParm
            End Get
            Set(ByVal Value As String)
                Me._successURLParm = Value
            End Set
        End Property

        Public ReadOnly Property UserName() As System.Web.UI.WebControls.TextBox
            Get
                Return DirectCast(Me.FindControl("UserName"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property

        Public ReadOnly Property Password() As System.Web.UI.WebControls.TextBox
            Get
                Return DirectCast(Me.FindControl("Password"), System.Web.UI.WebControls.TextBox)
            End Get
        End Property

        Public ReadOnly Property LoginMsg() As System.Web.UI.WebControls.Label
            Get
                Return DirectCast(Me.FindControl("LoginMsg"), System.Web.UI.WebControls.Label)
            End Get
        End Property
#End Region

#Region " Login Methods "
        Public Overridable Sub Login(ByVal redirectUrl As String)
            If Not redirectUrl Is Nothing AndAlso redirectUrl <> "" Then
                Login(False)
            Else
                Login(True)
            End If
        End Sub

        'Performs the login
        Public Overridable Sub Login(ByVal bRedirectOnSuccess As Boolean)
            Dim strUserName As String = Me.UserName.Text
            Dim strPassword As String = Me.Password.Text
            If (Me.Password.Text = "**********" Or Me.Password.Text = "") Then
                Dim state As SignInState = New SignInState
                strPassword = state.LoginPassword
            End If
            Dim errMessage As String = ""
            Dim clientIPAddress As String = Me.Page.Request.ServerVariables("REMOTE_ADDR") & " (HTML)"
            Dim strUserId As String = ""

            Dim bSuccess As Boolean = False
            Try
                'SetLoginInfo will do the work of authenticating the name and password
                bSuccess = DirectCast(Me.Page, BaseApplicationPage).CurrentSecurity.SetLoginInfo(strUserName, strPassword, errMessage)
            Catch ex As System.Threading.ThreadAbortException
                Throw
            Catch e As System.Exception
                ProcessLoginFailed(ERR_INTERNAL_ERROR & " " & e.Message, "")
            End Try

            'success!
            If (bSuccess) Then
                RaiseEvent LoginSucceeded(Me, New System.EventArgs())

                If bRedirectOnSuccess Then
                    RedirectOnSuccess()
                End If
            Else
                If Not errMessage Is Nothing AndAlso errMessage <> "" Then
                    ProcessLoginFailed(errMessage, strUserName)
                Else
                    ProcessLoginFailed(ERR_INVALID_LOGIN_INFO, strUserName)
                End If
            End If
        End Sub

        Protected Overridable Sub RedirectOnSuccess()
            If (Len(Trim(Me.SuccessURL)) > 0) Then
                Me.Page.Response.Redirect(Me.SuccessURL)
            Else
                CType(Me.Page, BaseClasses.Web.UI.BasePage).RedirectBack(True)
            End If
        End Sub

        Private Sub Reset()
        End Sub

        'Login failed, so redirect back to the login page passing information on the URL
        Protected Overridable Sub ProcessLoginFailed(ByVal message As String, _
                                ByVal userName As String)

            RaiseEvent LoginFailed(Me, New System.EventArgs())

            Dim url As String
            url = BaseClasses.Configuration.ApplicationSettings.Current.SignInPageUrl() & "?message=" & Me.Page.Server.UrlEncode(message)
            If Not Me.SuccessURLParam Is Nothing AndAlso Me.SuccessURLParam.Trim.Length > 0 Then
                url &= "&" & Me.SuccessURLParam & "=" & Me.SuccessURL
            End If

            If (Trim(userName) <> "") Then
                url = url & "&UserName=" & Trim(userName)
            End If
            url = url & "&mode=yes"
            DirectCast(Me.Page, BaseApplicationPage).SystemUtils.shouldRollBackTransaction = True
            CType(Me.Page, BaseClasses.Web.UI.BasePage).RemoveCurrentRequestFromSessionNavigationHistory()
            BaseClasses.Utils.NetUtils.SetCookie(BaseClasses.Utils.NetUtils.CookieAutoLogin(), "False")
            Dim Session As System.Web.SessionState.HttpSessionState = System.Web.HttpContext.Current.Session
            Session.Abandon()
            Me.Page.Response.Redirect(url)
            Me.Page.Response.End()
        End Sub
#End Region

#Region " Misc Methods "
        Protected Overrides Sub LoadViewState(ByVal savedState As Object)
            MyBase.LoadViewState(savedState)
            Me.SuccessURL = CStr(Me.ViewState.Item("SuccessURL"))
            Me.SuccessURLParam = CStr(Me.ViewState.Item("SuccessURLParam"))
        End Sub

        Protected Overrides Function SaveViewState() As Object
            Me.ViewState.Item("SuccessURL") = Me.SuccessURL
            Me.ViewState.Item("SuccessURLParam") = Me.SuccessURLParam
            Return MyBase.SaveViewState()
        End Function

        'Sets the text of the login message
        Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
            MyBase.OnDataBinding(e)

            Dim strMessage As String = Me.Page.Request.QueryString("Message")
            If Not (IsNothing(strMessage)) Then
                strMessage = strMessage.Replace("<br>", vbCrLf)
                strMessage = Me.Page.Server.HtmlEncode(strMessage)
            End If

            Me.UserName.Text = Me.Page.Request.QueryString("UserName")
            If Not (IsNothing(Me.UserName.Text)) Then
                Me.UserName.Text = Me.Page.Server.HtmlEncode(Me.UserName.Text)
            End If

            If Not Me.SuccessURLParam Is Nothing AndAlso Me.SuccessURLParam.Trim.Length > 0 Then
                Me.SuccessURL = Me.Page.Request.QueryString(Me.SuccessURLParam.Trim)
                If (Not Me.SuccessURL Is Nothing) Then
                    Me.SuccessURL = Me.SuccessURL.Trim()
                    Me.SuccessURL = Me.Page.Server.HtmlEncode(Me.SuccessURL)
                End If
            End If

            ' Set the Login Message
            If (Not (IsNothing(strMessage))) Then
                LoginMsg.Text = strMessage
            ElseIf (Not (IsNothing(Me.SuccessURL)) AndAlso Me.SuccessURL <> "") Then
                LoginMsg.Text = LOGIN_MSG_SESSION_INVALID
            Else
                LoginMsg.Text = LOGIN_MSG
            End If
        End Sub
#End Region

    End Class
#End Region
End Namespace