Results 1 to 2 of 2

Thread: Storing information in a cookie

  1. #1

    Thread Starter
    Addicted Member DigitalMyth's Avatar
    Join Date
    Nov 2002
    Location
    England..
    Posts
    169

    Storing information in a cookie

    I want to store a string in a cookie but i can't get it working.

    This is the script that works;

    VB Code:
    1. If Authenticated(txtuser.text,txtpass.text) then
    2.    
    3.     Dim ticket as New FormsAuthenticationTicket( txtuser.text, checkbox1.checked, 50 )
    4.     Dim encTicket As String = FormsAuthentication.Encrypt(ticket)
    5.     Dim cookie as New HttpCookie(FormsAuthentication.FormsCookieName,encticket)
    6.     'cookie("diruser") = "testpod"
    7.     Response.Cookies.Add(cookie)
    8.     response.redirect("Corefile.aspx?action=summary")
    9.     else
    10.    
    11.     Label1.text = "Bad username or Password"
    12.     end if
    13.     End Sub

    But as soon as I un-comment the

    cookie("diruser") = "testpod"

    line the asp.net doesn't think the cookie is vaild and redirects the user to the login page.

    My question is how do i store a value in a cookie and how to get it back?

    Thanks

  2. #2
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    This is how I set a login cookie:

    Code:
    Sub cmdLogin_Click(Sender As Object, E As EventArgs)
          if chkRem.checked=true then
          dim objcookie as new httpCookie("Username",txtlogin.text)
          objcookie.expires=datetime.maxvalue
          response.cookies.add(objcookie)
          dim objcookie2 as new httpCookie("UPass",txtPassword.text)
          objcookie2.expires=datetime.maxvalue
          response.cookies.add(objcookie2)
          dim objcookie3 as new httpCookie("RememberMe","Yes")
          objcookie3.expires=datetime.maxvalue
          response.cookies.add(objcookie3)
          else
          dim objcookie3 as new httpCookie("RememberMe","No")
          objcookie3.expires=datetime.maxvalue
          response.cookies.add(objcookie3)
          
          end if
    And then to read it back:
    Code:
    If Not Request.QueryString("logout") = "true" Then
                    Dim objcookie As HttpCookie = Request.Cookies("RememberMe")
                    If Not IsNothing(objcookie) Then
                        If Request.Cookies("RememberMe").Value.ToString = "Yes" Then
                            txtLogin.Text = Request.Cookies("Username").Value
                            txtPassword.Text = Request.Cookies("UPass").Value
                            Response.Write(Request.Cookies("UPass").Value)
                            chkrem.Checked = True
                        End If
                        Dim bUserID As Boolean
                        Dim validlog As QTIMLSNet.vlogin.login = New QTIMLSNet.vlogin.login
    
    
    
    
                        'user.identity.name=txtLogin.text
                        'Attempt to Validate User Credentials using CustomersDB  
                        bUserID = validlog.CheckLogin(txtLogin.Text, txtPassword.Text)
    
                        If (bUserID = True) Then
                            ' Redirect browser back to originating page
                            checkdefaults()
                            fillsession()
                            FormsAuthentication.RedirectFromLoginPage("Jason Baxter", False)
                        Else ' Login failed
                            Session.Abandon()
                        End If
    "Find all you need in your mind if you take the time" -DT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width