Imports System.Web.Security
Imports ASPNET.StarterKit.TimeTracker.BusinessLogicLayer

Public MustInherit Class SignIn
    Inherits System.Web.UI.UserControl
    Protected WithEvents email As System.Web.UI.WebControls.TextBox
    Protected WithEvents password As System.Web.UI.WebControls.TextBox
    Protected WithEvents RememberCheckbox As System.Web.UI.WebControls.CheckBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents register As System.Web.UI.WebControls.Button
    Protected WithEvents Btnsignin As System.Web.UI.WebControls.Button
    Protected WithEvents Message As System.Web.UI.WebControls.Label

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ID = "Btnsignin"

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub register_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles register.Click
        Response.Redirect("Register.aspx", False)
    End Sub

    Private Sub Btnsignin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnsignin.Click
        ' Attempt to Validate User Credentials using UsersDB
        Dim accountSystem As New TTUser()
        Dim userId As String = accountSystem.Login(email.Text, TTSecurity.Encrypt(password.Text))

        If Not (userId Is Nothing) And userId <> "" Then
            ' Use security system to set the UserID within a client-side Cookie
            FormsAuthentication.SetAuthCookie(email.Text, RememberCheckbox.Checked)
            ' Redirect browser back to originating page
            FormsAuthentication.RedirectFromLoginPage(email.Text, False)
        Else
            Message.Text = "<" & "br" & ">Login Failed!" & "<" & "br" & ">"
        End If
    End Sub
End Class
