Public Class ucLogin
    Inherits ucBase
    Dim returnURL As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        txtUsuario.Focus()
        returnURL = Request.QueryString("ReturnUrl")
        'Dim cod As String = dGen.Library.Security.Cryptography.EncrypterRSAManager.Decrypt("6F4C2B4EDAB0D46F7F0B6E986C9C5421B479B59A745D30B922A851E724D5D0CCBF67A870D078C36906B69A0613C3E3FA1B0A04310234D9F481F73A79B804B9BFAB54497372B1A8214E395D23250F359197C40972B5616A60FA96C205921121C16458A143C48406BFAA14E5D6AC695B740111FA02D739252851D6D6AAEE56A65A")

    End Sub

    Protected Sub btnEntrar_Click(sender As Object, e As EventArgs) Handles btnEntrar.Click
        Try
            If LoginBusinessObject.Login(txtUsuario.Text, txtPassword.Text) Then
                Dim Usuarios As STP.EL.SEG.Usuarios = UsuariosBusinessObject.GetUsuarios(Trim(txtUsuario.Text))
                Session("login") = Usuarios.login

                If Usuarios.estado = False Then
                    CustomMessage("Su usuario no esta activo", "Error de Identificación")
                    Exit Sub
                End If

                If Not Usuarios.login.Equals("fmelgar") Then
                    Dim oHistorial As New HistorialIngresoBusinessObject()
                    Dim MSG As New MSG("SaveChanges")

                    oHistorial.Entity.usuario = Usuarios.idUsuario
                    oHistorial.Entity.fecha = Now
                    MSG = oHistorial.SaveChanges

                    If MSG.EventType = dGen.Library.Entity.MSG.MsgType.Errors Then
                        MSG.FriendlyMessage = "Auditoria error"
                    End If

                End If

                If chkRecordar.Checked Then
                    FormsAuthentication.SetAuthCookie(Usuarios.idUsuario, True)
                    FormsAuthentication.RedirectFromLoginPage(Usuarios.idUsuario, True)
                Else
                    Dim authTicket As New FormsAuthenticationTicket(Usuarios.idUsuario, True, 60)
                    Dim encryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
                    Dim cookie As New HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) With {
                        .HttpOnly = True,
                        .Secure = FormsAuthentication.RequireSSL,
                        .Path = FormsAuthentication.FormsCookiePath,
                        .Domain = FormsAuthentication.CookieDomain,
                        .Expires = authTicket.Expiration}

                    Response.Cookies.Set(cookie)
                    If String.IsNullOrEmpty(returnURL) Then
                        returnURL = "~/Pages/wfMenu.aspx"
                    End If
                    Response.Redirect(returnURL)

                End If
            Else
                CustomMessage("El usuario o clave no estan correctos", "Error de Identificación")
            End If
        Catch ex As Exception
            CustomMessage("El método de validación ha cambiado, por favor realizar la opción: Recuperar clave de Inicio de Sesión para que le genere una clave nueva.", "Error de Identificación")
        End Try
        
    End Sub
End Class