﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Net
Imports System.Reflection
Imports HashLibrary

Partial Class login
    Inherits System.Web.UI.Page
    Public ErrorText As String = ""

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ErrorText = ""
        Session("Key") = ConfigurationManager.AppSettings("KEY").ToString
        If Not Page.IsPostBack Then
            If Not IsNothing(Request.QueryString("logout")) Then
                If Request.QueryString("logout") = 1 Then
                    functions.ClearSessions()
                End If
            End If

            If Not IsNothing(Session("userid")) Then
                Response.Redirect("dashboard.aspx")
            End If

            functions.ClearSessions()

            Dim cookie As HttpCookie = Request.Cookies("UserInfo")
            If Not cookie Is Nothing AndAlso Not cookie("email") Is Nothing Then
                email2.Value = cookie("email")
                RememberMe.Checked = True
            End If
        End If
    End Sub


    Private Sub btnSignIn_ServerClick(sender As Object, e As EventArgs) Handles btnSignIn.ServerClick

        Dim authenticated As Boolean = ValidateCredentials(email2.Value, password2.Value)

        If authenticated Then
            Dim cookie As New HttpCookie("UserInfo")
            cookie("email") = email2.Value
            cookie("password") = password2.Value

            cookie.Expires = Date.Now.AddDays(365)
            Response.Cookies.Add(cookie)

            Response.Redirect("Dashboard.aspx")

        Else
            ErrorText = "Invalid login credentials"
        End If
    End Sub

    Private Function ValidateCredentials(ByVal email As String, ByVal password As String) As Boolean
        Dim returnValue As Boolean = False
        Session("USER_PK") = Nothing
        If email.Length > 3 AndAlso password.Length > 2 Then

            Try
                Dim dt As DataTable = GetDataWithPassword(email, password)
                If Not IsNothing(dt) Then
                    Dim userid As Integer = CInt(dt.Rows(0)("userid"))
                    If userid > 0 Then
                        Session("userid") = userid
                        Session("REP_ID") = 0
                        Session("DisplayName") = dt.Rows(0)("DisplayName").ToString
                        Session("DSVAccount") = dt.Rows(0)("DSVAccount").ToString
                        Session("DSVAccountStatus") = dt.Rows(0)("DSVAccountStatus")
                        Session("CashAccount") = dt.Rows(0)("CashAccount")
                        Session("ReceiveStatements") = dt.Rows(0)("ReceiveStatements")

                        returnValue = True
                    End If
                End If

            Catch ex As Exception

            Finally
                'If conn IsNot Nothing Then conn.Close()
            End Try
        Else
        End If

        Return returnValue
    End Function

    Private Function GetDataWithPassword(ByVal email As String, ByVal password As String) As DataTable
        Try
            Using con As SqlConnection = New SqlConnection(functions.GetConnectionString)
                Using cmd As SqlCommand = New SqlCommand("USERS_spLOGIN", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@UserEmail", email.Trim)
                    cmd.Parameters.AddWithValue("@Password", password.Trim)
                    If con.State = ConnectionState.Closed Then con.Open()
                    Dim dt As New DataTable
                    Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                        da.Fill(dt)
                        If dt Is Nothing Then
                            Return Nothing
                        Else
                            If dt.Rows.Count = 0 Then
                                Return Nothing
                            Else
                                Return dt
                            End If
                        End If
                    End Using
                End Using
            End Using
        Catch ex As Exception
            Dim s As String
            s = ex.Message
        End Try
    End Function

    Private Function GetDataSSO(ByVal email As String) As DataTable
        Try
            Using con As SqlConnection = New SqlConnection(functions.GetConnectionString)
                Using cmd As SqlCommand = New SqlCommand("USERS_spSSO", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@UserEmail", email.Trim)
                    If con.State = ConnectionState.Closed Then con.Open()
                    Dim dt As New DataTable
                    Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                        da.Fill(dt)
                        If dt Is Nothing Then
                            Return Nothing
                        Else
                            If dt.Rows.Count = 0 Then
                                Return Nothing
                            Else
                                Return dt
                            End If
                        End If
                    End Using
                End Using
            End Using
        Catch ex As Exception
            Dim s As String
            s = ex.Message
        End Try
    End Function

    Private Sub btnSignIn2_ServerClick(sender As Object, e As EventArgs) Handles btnSignIn2.ServerClick

        Dim cookie As HttpCookie = Request.Cookies("UserInfo")
        If Not cookie Is Nothing AndAlso Not cookie("email") Is Nothing Then

            Dim dt As DataTable = GetDataSSO(cookie("email"))
            If Not IsNothing(dt) Then
                Dim userid As Integer = CInt(dt.Rows(0)("userid"))
                If userid > 0 Then
                    Session("userid") = userid
                    Session("REP_ID") = 0
                    Session("DisplayName") = dt.Rows(0)("DisplayName").ToString
                    Session("DSVAccount") = dt.Rows(0)("DSVAccount").ToString
                    Session("DSVAccountStatus") = dt.Rows(0)("DSVAccountStatus")
                    Session("CashAccount") = dt.Rows(0)("CashAccount")
                    Session("ReceiveStatements") = dt.Rows(0)("ReceiveStatements")

                    Response.Redirect("Dashboard.aspx")

                End If
            End If
        Else
            Dim Ncookie As New HttpCookie("UserInfo")
            Ncookie("email") = email2.Value
            Ncookie.Expires = Date.Now.AddDays(365)
            Response.Cookies.Add(Ncookie)
        End If
    End Sub

End Class
