Results 1 to 12 of 12

Thread: cookies question

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    cookies question

    how do i create cookies that endure forever? and how to read them? i tried the following:
    Code:
    DateTime date = new DateTime(9999, 12, 20);
    HttpCookie userCookie = new HttpCookie("user", "xxxxxxx");
    HttpCookie pwCookie = new HttpCookie("pw", "xxxxxx");
    userCookie.Expires = date;
    pwCookie.Expires = date;
    Response.SetCookie(new HttpCookie("user", "xxxxxx"));
    Response.SetCookie(new HttpCookie("pw", "xxxxxx"));
    and then in another page tried to load it
    Code:
    Label1.Text = Request.Cookies["user"].Value;
    Label2.Text = Request.Cookies["pw"].ToString();
    but i get a error when a object doesnt exist..what am i doing wrong?

    tks
    \m/\m/

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try something like this:
    VB Code:
    1. 'writing to the cookie
    2.             Dim ckInfo As New HttpCookie("MHCPrivate")
    3.             ckInfo.Values.Add("Username", txtUsername.Text)
    4.             ckInfo.Values.Add("Password", txtPassword.Text)
    5.             ckInfo.Values.Add("LastVisit", Now.Date)
    6.             ckInfo.Values.Add("RememberMe", chkPersist.Checked)
    7.             ckInfo.Expires = DateTime.MaxValue 'MaxValue is pretty much Forever
    8.             Response.AppendCookie(ckInfo)
    9.  
    10. 'reading from the cookie
    11.             Dim ckInfo As HttpCookie
    12.             ckInfo = Request.Cookies("MHCPrivate")
    13.             'You might add some error handling here to make sure the cookie is not nothing
    14.             txtUsername.Text = ckInfo.Values("Username")
    15.             txtPassword.Text = ckInfo.Values("Password")

  3. #3

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks thu i had already discovered a way of doing it..but i am facing a problem that i think that has nothing to do with cookies..
    i have a page called Log.aspx which has a label that says the nick of the person logged in. then i have another page called Login.aspx. the Login.aspx has a textbox to put the nick and another to put the password and a LOGIN button. if i login and go to the Log.aspx it shows the nick..now if i go to the Login.aspx as it detects the cookies the Login button will be replaced with a LogOut button. when i click it and go to the Log.aspx it shows no nick now..but now if i go back to the Log.aspx it will still show the button to Log Out..what am i doing wrong?

    LOGIN.aspx
    ON_LOAD
    Code:
    if (Request.Cookies["WebLog"] != null && Request.Cookies["WebLog"]["User"] != null) {
    nickTxt.Text = Request.Cookies["WebLog"]["User"];
    pwTxt.Text = Request.Cookies["WebLog"]["Pw"];
    nickTxt.Enabled = false;
    pwTxt.Enabled = false;
    LogButton.Text = "Log Out";
    }
    Code:
    private void LogOn_Click(object sender, System.EventArgs e) {
    if (LogButton.Text == "Log Out") {
    Response.Cookies["WebLog"].Values.Clear();
    } else {
    if ((nickTxt.Text == "xxx" && pwTxt.Text == "xxx") ||
    (nickTxt.Text == "xxx" && pwTxt.Text == "xxx") ||
    (nickTxt.Text == "xxx" && pwTxt.Text == "xxx")) {
    Response.Cookies["WebLog"]["User"] = nickTxt.Text;
    Response.Cookies["WebLog"]["Pw"] = pwTxt.Text;
    Response.Cookies["WebLog"].Expires = new DateTime(9999, 12, 25);
    }
    }
    Response.Redirect("Log.aspx");
    }
    the Log.aspx page code i think doesnt need to be shown as it is irrelevant. note also that here i changed the nicks and respective passwords to xxx.
    you can check if u want the page at:
    http://localhost/213.228.157.144/Log.aspx
    http://localhost/213.228.157.144/Login.aspx
    (user VB, pw FORUMS)
    \m/\m/

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I got 404 errors on those pages even after i removed localhost. Maybe its not refreshing try setting an else in there to set it back to login if the cookie is missing.

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    \m/\m/

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i will reboot back in 3min
    \m/\m/

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    back
    \m/\m/

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Did you change something? It always says Login for me. For testing you sohuld have a link from the log page back to login.

  9. #9

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i think it was because i was changing url thru IE gay so that was happening that "bug"

    tks anyways!
    \m/\m/

  10. #10

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    hmm didnt see it well..it has the error in the same way..go to the page, login, then in the main page go to LogOut, then log out and then it will appear to u to LogOut when it should appear to login! but if u close IE and open it it will show login!
    \m/\m/

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't see any LogOut on the log page?

  12. #12

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    tks its working now..the logout was the "carregue aqui" that is in bold i made it by puting the cookie expire date to DateTime.Now..using the Cookies.Remove(myCookie), Cookies[myCookie].Values.Clear() and Cookies.Clear() wasnt working i still dont understand why because in another project was working great
    \m/\m/

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