简体   繁体   English

为什么我的Cookie在C#ASP.NET空Web项目中显示为null?

[英]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.

相关问题 Cookie 总是 Null C# ASP.NET MVC - Cookie is always Null C# ASP.NET MVC ASP.NET 核心 web 应用程序项目文件没有显示(该项目显示为空,即使它是 model 控制的...) 为什么? - ASP.NET Core web application project files are not showing up (the project appears empty even though it is of model control...) Why? 在数据库中显示为空的行-ASP.NET C# - Rows showing up as null in database - asp.net c# 为什么从asp.net c#应用程序进行的Web服务调用返回null? - why the web service call from asp.net c# application returns null? cookie第一次显示空值,再次到达该页面后,我可以看到cookie值。 Asp.net c# - cookie is showing null value for first time and after coming again to that page, i can see cookie value. Asp.net c# 有人可以告诉我为什么我的标题没有显示asp.net C# - Can someone tell me why my header isn't showing up asp.net c# ASP.NET Web项目(C#)是否有按需编译选项? - Is there a compile on demand option for an ASP.NET Web Project (C#)? C#-asp.net //为什么此值会为NULL? - c# - asp.net // why does this value get NULL? 为什么在gridview上进行更新时不会在asp.net c#Web窗体应用程序中更新数据库? - Why update on gridview is not updating the database in my asp.net c# web forms application? ASP.NET C#空TextBox控件作为空字符串保存到数据库,我需要另存为null - ASP.NET C# empty TextBox controls are saving to the database as empty strings, I need to save as null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM