Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports DevExpress.ExpressApp.Web
Imports Nuvolar.Module.Web
Imports System.Web
Imports DevExpress.Xpo.DB.Helpers
Imports DevExpress.ExpressApp
Imports Nuvolar.Module
Imports System.Web.Security
Imports DevExpress.Xpo.DB
Imports DevExpress.Xpo
Imports DevExpress.ExpressApp.Security
Imports System.Net
Imports System.IO

Partial Public Class NuvolarAspNetApplication
    Inherits WebApplication
    Protected Overrides Sub ReadSecuredLogonParameters()
        MyBase.ReadSecuredLogonParameters() ' the "UserName" is restored in the base method.
    End Sub
    Private canReadSecuredLogonParameters_Renamed As Boolean = True
    Protected Overrides Function CanReadSecuredLogonParameters() As Boolean
        If (Not canReadSecuredLogonParameters_Renamed) Then
            Return False
        End If
        Return MyBase.CanReadSecuredLogonParameters()
    End Function
    Protected Overrides Function OnLogonFailed(ByVal logonParameters As Object, ByVal e As Exception) As Boolean
        If CanReadSecuredLogonParameters() Then
            FormsAuthentication.SignOut()
            canReadSecuredLogonParameters_Renamed = False
            Try
                Start()
            Finally
                canReadSecuredLogonParameters_Renamed = True
            End Try
            Return True
        Else
            Return MyBase.OnLogonFailed(logonParameters, e)
        End If
    End Function
    Protected Overrides Sub ReadLastLogonParametersCore(ByVal storage As DevExpress.ExpressApp.Utils.SettingsStorage, ByVal logonObject As Object)
        Dim standardLogonParameters As AuthenticationStandardLogonParameters = TryCast(logonObject, AuthenticationStandardLogonParameters)
        If (standardLogonParameters IsNot Nothing) AndAlso String.IsNullOrEmpty(standardLogonParameters.UserName) Then
            MyBase.ReadLastLogonParametersCore(storage, logonObject)
            Try
                TryCast(logonObject, ChangeLogon).DBConnection = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnCookies).LoadOption("", "DBConnection")
                TryCast(logonObject, ChangeLogon).SQLServer = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnCookies).LoadOption("", "SQLServer")
                TryCast(logonObject, ChangeLogon).Company = TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnCookies).LoadOption("", "Company")
                Select Case TryCast(storage, DevExpress.ExpressApp.Utils.SettingsStorageOnCookies).LoadOption("", "SQLType")
                    Case "MSSQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.MSSQL
                    Case "PostgreSQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.PostgreSQL
                    Case "MySQL"
                        TryCast(logonObject, ChangeLogon).SQLType = eSQLType.MySQL
                End Select
            Catch
            End Try
        End If
    End Sub
    Protected Overrides Sub OnLoggingOn(ByVal args As LogonEventArgs)
        Dim connstr As String = ""
        Dim _ChangeLogon As ChangeLogon
        Dim _plattform As String = ""
        MyBase.OnLoggingOn(args)

        _ChangeLogon = TryCast(args.LogonParameters, ChangeLogon)

        GLOBAL_Company = _ChangeLogon.Company

        Me.ConnectionString = GetSISSecurity()

    End Sub
    Private Function GetSISSecurity() As String
        Dim _ConnectionString As String = ""
        Try

            Dim _url = "http://nuvolar.azurewebsites.net/SISSecuritySSL.ashx?id=" & UCase(GLOBAL_Company)
            Dim _request As HttpWebRequest = TryCast(WebRequest.Create(_url), HttpWebRequest)
            Dim _response As HttpWebResponse = TryCast(_request.GetResponse, HttpWebResponse)
            Dim _reader As StreamReader = New StreamReader(_response.GetResponseStream())
            _ConnectionString = _reader.ReadToEnd

            _ConnectionString = Encoding.UTF8.GetString(Convert.FromBase64String(_ConnectionString))
            _ConnectionString = AESDecrypt(_ConnectionString, "HrX12!!0Zm7764W2", "vXm657YS+!0@mmT")

        Catch ex As Exception

        End Try
        Return _ConnectionString
    End Function
End Class
