Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net Cookies that apply to all web pages on site

I use the following code to log a user of my ASP.net/SQL web app in automatically as the page loads.
It does this by using cookies that remembers the username and password.

I have done this on every page of the site but would rather have it that if the user logs in on any page that
they can come back to any page and the cookies will remember their credentials

Is there a way to have the cookies remember the details no matter which page they come back to after logging in on just one page?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then

                If ((Not (Request.Cookies("xUserName")) Is Nothing) AndAlso (Not (Request.Cookies("xPassword")) Is Nothing)) Then

                    'txtPassword.Attributes("value") = Request.Cookies("Password").Value
                    Me.txtUsername.Text = Request.Cookies("xUserName").Value
                    Me.txtPassword.Text = Request.Cookies("xPassword").Value
                    Call LogIn(False)

                End If

                Me.DropDownList_Number_Days.Text = 365
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub



    Sub LogIn(ByVal blnManualLogin As Boolean)

        Dim cs As String = ConfigurationManager.ConnectionStrings("PSQL").ConnectionString

        Dim cn As New SqlConnection(cs)


        Dim sSQL As String
        sSQL = "Select * From Users1"
        Dim cmd As New SqlCommand(sSQL, cn)
        Dim oUsername, oPassword, oName, oEmployeeID, oLevel, oTables As String

        Dim blnWorked As Boolean = False
        Try

            '// open the connection
            cn.Open()

            '// execute the sql statement
            Using reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                While reader.Read()
                    '// this loops through all of the returned records
                    'Response.Write("blah")
                    oUsername = reader("Username").ToString()
                    oPassword = reader("Password").ToString()
                    oName = reader("Name").ToString()
                    oLevel = reader("Level").ToString()
                    oTables = reader("Tables").ToString()
                    oEmployeeID = reader("EmployeeID").ToString()

                    If Me.txtUsername.Text = oUsername And Me.txtPassword.Text = oPassword Then


                        If blnManualLogin = True Then
                            'Use cookie info for manual log in---------
                            If Me.DropDownList_Number_Days.Text = "0" Then
                                Response.Cookies("xUserName").Expires = DateTime.Now.AddDays(-1)
                                Response.Cookies("xPassword").Expires = DateTime.Now.AddDays(-1)
                            Else
                                Dim oNumDays As Integer = CInt(Me.DropDownList_Number_Days.Text)
                                Response.Cookies("xUserName").Expires = DateTime.Now.AddDays(oNumDays)
                                Response.Cookies("xPassword").Expires = DateTime.Now.AddDays(oNumDays)
                            End If

                            Response.Cookies("xUserName").Value = txtUsername.Text.Trim
                            Response.Cookies("xPassword").Value = Me.txtPassword.Text.Trim
                            '-----------------------
                        End If

                        Me.Panel_Login.Visible = False
                        Me.Panel_Order.Visible = True
                        'Load_Drop_Down_Unique_Items("Category", "Order Items", "", 0, Me.DropDownList_OrderCategories, True, False) 'Load all shafts to drop down
                        'Load Order Categories

                        Me.lblName.Text = oName
                        Me.lblEmployeeID.Text = oEmployeeID

                        Exit Sub
                    End If

                End While

            End Using


        Catch ex As Exception

            Response.Write(ex.Message & " xv5")

        Finally
            If cn.State <> ConnectionState.Closed Then
                cn.Close()
            End If
        End Try
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JS List
JS List
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks very much