简体   繁体   中英

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. I cannot get it to work properly. When exiting the browser and reloading the webpage, the cookie is always null.

I declared the cookie as a static variable so I can access in a later method to send it to the client.

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().

    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.

Cookies are non-persistent by default. That means as longas you don't specify an expiration date for the cookie the browser clears it, when you close the browser.

So in this case you'll need a persistent cookie , which can be created by setting the 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

But note what @CodeCaster already said: A cookie is only a small piece of text which can be easily modified by the client. So you should consider storing sensitive information elsewhere. Or at least you should consider encrypting your cookies.

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.

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

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM