Ejemplo n.º 1
0
    protected void Page_Init(object sender, EventArgs e)
    {
        // -- OUR CODE

        // Cookie reading
        DataTable cartTable = new DataTable();
        cartTable.Columns.Add("Albums", typeof(string));
        XmlDocument xmldoc2 = new XmlDocument();
        foreach (string cookieName in Request.Cookies.AllKeys)
        {
            HttpCookie cookie = Request.Cookies[cookieName];
            if (cookie.Name.Contains("cart_"))
            {
                int amount = int.Parse(cookie["amount"]);
                Album album = new Album();
                album = album.findById(int.Parse(cookie["id"]), Server);

                DataRow dtrow = cartTable.NewRow();
                dtrow["Albums"] = album.Artist + " - " + album.Title + " (" + amount + " x " + album.Price + " kr.)";
                cartTable.Rows.Add(dtrow);
            }
        }
        GridViewCart.DataSource = cartTable;
        GridViewCart.DataBind();

        // -- OUR CODE END

        // The code below helps to protect against XSRF attacks
        var requestCookie = Request.Cookies[AntiXsrfTokenKey];
        Guid requestCookieGuidValue;
        if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
        {
            // Use the Anti-XSRF token from the cookie
            _antiXsrfTokenValue = requestCookie.Value;
            Page.ViewStateUserKey = _antiXsrfTokenValue;
        }
        else
        {
            // Generate a new Anti-XSRF token and save to the cookie
            _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
            Page.ViewStateUserKey = _antiXsrfTokenValue;

            var responseCookie = new HttpCookie(AntiXsrfTokenKey)
            {
                HttpOnly = true,
                Value = _antiXsrfTokenValue
            };
            if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
            {
                responseCookie.Secure = true;
            }
            Response.Cookies.Set(responseCookie);
        }

        Page.PreLoad += master_Page_PreLoad;
    }