最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Not all cookies are being returned with request.Cookies - Stack Overflow

programmeradmin8浏览0评论

I have a project that track two shopping carts and I have cookies set up to track what is in each cart "shopping_cart" and "sc_shopping_cart". However when I try to read the cookies in my razor page using request.cookies only "shopping_cart" shows up. If I check in my browser, both of the cookies are set. Visual Studio Locals, Chrome=>Storage=>Cookies

The strange thing is that I can read and update "sc_shopping_cart" through javascript.

Here is C# code I'm using:

        public static Dictionary<int,int> GetScCartDictionary(HttpRequest request, HttpResponse response)
    {
        string cookieValue = request.Cookies["sc_shopping_cart"] ?? "";

        try
                                                                                                                                                                                                     {
            var cart = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(cookieValue));
            Console.WriteLine("[CartHelper] cart=" + cookieValue + " -> " + cart);
            var dictionary = JsonSerializer.Deserialize<Dictionary<int, int>>(cart);
            if(dictionary != null)
            {
                return dictionary;
            }
        }
        catch (Exception)
        {

        }

        if(cookieValue.Length > 0)
        {
            // this cookie is not valid => delete it
            response.Cookies.Delete("sc_shopping_cart");
        }

        return new Dictionary<int, int>();
    }

Any help is greatly appreciated.

Thanks, Dave

I have a project that track two shopping carts and I have cookies set up to track what is in each cart "shopping_cart" and "sc_shopping_cart". However when I try to read the cookies in my razor page using request.cookies only "shopping_cart" shows up. If I check in my browser, both of the cookies are set. Visual Studio Locals, Chrome=>Storage=>Cookies

The strange thing is that I can read and update "sc_shopping_cart" through javascript.

Here is C# code I'm using:

        public static Dictionary<int,int> GetScCartDictionary(HttpRequest request, HttpResponse response)
    {
        string cookieValue = request.Cookies["sc_shopping_cart"] ?? "";

        try
                                                                                                                                                                                                     {
            var cart = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(cookieValue));
            Console.WriteLine("[CartHelper] cart=" + cookieValue + " -> " + cart);
            var dictionary = JsonSerializer.Deserialize<Dictionary<int, int>>(cart);
            if(dictionary != null)
            {
                return dictionary;
            }
        }
        catch (Exception)
        {

        }

        if(cookieValue.Length > 0)
        {
            // this cookie is not valid => delete it
            response.Cookies.Delete("sc_shopping_cart");
        }

        return new Dictionary<int, int>();
    }

Any help is greatly appreciated.

Thanks, Dave

Share Improve this question edited Apr 2 at 5:38 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 31 at 20:24 David SlishDavid Slish 112 bronze badges New contributor David Slish is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

The issue might be related to cookie scope, SameSite policy, or HttpOnly settings. You can use F12 developer tools to check them:

If the issue related it, you could change the cookie setting via the CookieOptions:

        Response.Cookies.Append("sc_shopping_cart", "sc shopping_cart value", new CookieOptions
        {
            HttpOnly = true, // Prevent JavaScript access (helps against XSS)
            Secure = true,   // Only sent over HTTPS
            SameSite = SameSiteMode.Strict, // Prevent CSRF attacks
            Expires = DateTime.UtcNow.AddHours(1)
        });

Besides, you can also try to use the F12 Network Tab to check the request header and confirm the cookie is being sent.

发布评论

评论列表(0)

  1. 暂无评论