[英]Why is my cookie showing as null in C# ASP.NET Empty Web Project?
Working on a Black Jack game and I am trying to save the Player's balance as a cookie. 在玩Black Jack游戏时,我试图将玩家的余额保存为cookie。 I cannot get it to work properly.
我无法使其正常工作。 When exiting the browser and reloading the webpage, the cookie is always null.
退出浏览器并重新加载网页时,cookie始终为null。
I declared the cookie as a static variable so I can access in a later method to send it to the client. 我将cookie声明为静态变量,因此可以在以后的方法中将其发送给客户端。
public partial class BlackJack : System.Web.UI.Page
{
public static HttpCookie cookie;
protected void Page_Load(object sender, EventArgs e)
{
cookie = Request.Cookies["Balance"];
if (!IsPostBack)
{
if (cookie != null)
{
PlayerBalance = Convert.ToInt32(cookie.Values["balance"]);
if (PlayerBalance == 0)
{
PlayerBalance = 250;
}
}
else
{
PlayerBalance = 250;
HttpCookie cookie = new HttpCookie("Balance");
cookie.Values.Add("balance", PlayerBalance.ToString());
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
}
PlayerBet = 0;
}
Then in a later method that runs after each hand, I save the cookie with Response.Cookies.Add(). 然后,在每手之后运行的更高版本的方法中,我使用Response.Cookies.Add()保存cookie。
public void Other Method()
{
cookie = Request.Cookies["Balance"];
cookie.Values["balance"] = PlayerBalance.ToString();
Response.Cookies.Add(cookie);
}
But if I close out of a browser and return to the site, the cookie is always null. 但是,如果我关闭浏览器并返回该站点,则cookie始终为null。
Cookies are non-persistent by default. 默认情况下,Cookie是非永久性的。 That means as longas you don't specify an expiration date for the cookie the browser clears it, when you close the browser.
这意味着只要您不指定Cookie的到期日期,浏览器就会在您关闭浏览器时将其清除。
So in this case you'll need a persistent cookie , which can be created by setting the Expires
-property: 因此,在这种情况下,您将需要一个持久性cookie ,可以通过设置
Expires
-property来创建它:
var cookie = new HttpCookie("Balance");
cookie.Expires = DateTime.Now.AddDays(1);
For more details have a look at this comprehensive article: https://msdn.microsoft.com/en-us/library/ms178194.aspx 有关更多详细信息,请参见这篇综合文章: https : //msdn.microsoft.com/zh-cn/library/ms178194.aspx
But note what @CodeCaster already said: A cookie is only a small piece of text which can be easily modified by the client. 但是请注意@CodeCaster已经说过:Cookie只是一小段文本,客户端可以轻松修改。 So you should consider storing sensitive information elsewhere.
因此,您应该考虑将敏感信息存储在其他位置。 Or at least you should consider encrypting your cookies.
或者至少您应该考虑对Cookie进行加密。
Remove the line 删除线
public static HttpCookie cookie;
It will create a non-thread safe type of cookie .In mutitreaded environment it will have mess up value. 它将创建非线程安全类型的cookie。在经过多线程读取的环境中,它将具有混乱的价值。
This works fine..Your static
causes the problem.Create cookie every every method and Dump it on browser Response.Cookies.Add(cookie)
with same name 您的
static
问题会导致此问题。每个方法都创建cookie并将其转储到浏览器Response.Cookies.Add(cookie)
且名称相同
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("Balance");
cookie.Values.Add("balance", "akash".ToString());
cookie.Expires = DateTime.Now.AddYears(1);
Response.Cookies.Add(cookie);
}
protected void Button1_Click(object sender, EventArgs e)
{
var cookie = Request.Cookies["Balance"];
cookie.Values["balance"] = "ggg".ToString();
Response.Cookies.Add(cookie);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.