|
-
Jul 22nd, 2003, 02:31 PM
#1
Thread Starter
yay gay
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/
-
Jul 22nd, 2003, 05:40 PM
#2
Try something like this:
VB Code:
'writing to the cookie
Dim ckInfo As New HttpCookie("MHCPrivate")
ckInfo.Values.Add("Username", txtUsername.Text)
ckInfo.Values.Add("Password", txtPassword.Text)
ckInfo.Values.Add("LastVisit", Now.Date)
ckInfo.Values.Add("RememberMe", chkPersist.Checked)
ckInfo.Expires = DateTime.MaxValue 'MaxValue is pretty much Forever
Response.AppendCookie(ckInfo)
'reading from the cookie
Dim ckInfo As HttpCookie
ckInfo = Request.Cookies("MHCPrivate")
'You might add some error handling here to make sure the cookie is not nothing
txtUsername.Text = ckInfo.Values("Username")
txtPassword.Text = ckInfo.Values("Password")
-
Jul 22nd, 2003, 05:48 PM
#3
Thread Starter
yay gay
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/
-
Jul 22nd, 2003, 05:53 PM
#4
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.
-
Jul 22nd, 2003, 05:56 PM
#5
Thread Starter
yay gay
\m/  \m/
-
Jul 22nd, 2003, 06:04 PM
#6
Thread Starter
yay gay
i will reboot back in 3min
\m/  \m/
-
Jul 22nd, 2003, 06:10 PM
#7
Thread Starter
yay gay
\m/  \m/
-
Jul 22nd, 2003, 06:13 PM
#8
Did you change something? It always says Login for me. For testing you sohuld have a link from the log page back to login.
-
Jul 22nd, 2003, 06:19 PM
#9
Thread Starter
yay gay
i think it was because i was changing url thru IE gay so that was happening that "bug"
tks anyways!
\m/  \m/
-
Jul 22nd, 2003, 07:01 PM
#10
Thread Starter
yay gay
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/
-
Jul 23rd, 2003, 12:22 AM
#11
I don't see any LogOut on the log page?
-
Jul 23rd, 2003, 12:24 AM
#12
Thread Starter
yay gay
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|