这是我在Login.aspx中的代码
protected void LoginButton_Click(object sender, EventArgs e)
{
HttpCookie loginCookie1 = new HttpCookie("loginCookie");
Response.Cookies["loginCookie1"].Value = LoginUser.UserName;
Response.Cookies.Add(loginCookie1);
}
这是在shop.aspx
protected void btnAddCart_Click(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie(dvProduct.DataKey.Value.ToString());
myCookie["Category"] = dvProduct.DataKey["Category"].ToString();
myCookie["Product"] = dvProduct.DataKey["Product"].ToString();
myCookie["Quantity"] = txtQuantity.Text;
myCookie["Price"] = dvProduct.DataKey["Price"].ToString();
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);
Response.Redirect("ViewCart.aspx", true);
}
我想从cookie中读取用户名的值(在login.aspx中设置的值
答案 0 :(得分:14)
你基本上需要请求cookie,它在你的页面上并不重要 这是关于cookies的解释
http://msdn.microsoft.com/en-us/library/ms178194.aspx
HttpCookie aCookie = Request.Cookies["loginCookie"];
string username = Server.HtmlEncode(aCookie.Value);
答案 1 :(得分:5)
设置loginCookie的代码看起来很奇怪:
HttpCookie loginCookie1 = new HttpCookie("loginCookie");
Response.Cookies["loginCookie1"].Value = LoginUser.UserName; // <--- strange!!!!
Response.Cookies.Add(loginCookie1);
您的Cookie很可能无法发送到浏览器 - 请使用Fiddler等HTTP调试程序进行检查。
答案 2 :(得分:1)
这应该这样做:
var userName = Request.Cookies["loginCookie"].Value;