Пример #1
0
        public HomeController(IHttpContextAccessor httpContextAccessor)
        {
            this._httpContextAccessor = httpContextAccessor;
            service = new DataRetrievalService();
            ivm     = new IndexViewModel();
            string userid = httpContextAccessor.HttpContext.Request.Cookies["userCookie"];

            if (userid == null || userid == "")
            {
                if (httpContextAccessor.HttpContext.Request.Cookies["sessionCookie"] == null || httpContextAccessor.HttpContext.Request.Cookies["sessionCookie"] == "" || httpContextAccessor.HttpContext.Request.Cookies["sessionCookie"].Length <= 5)
                {
                    string        guid    = Guid.NewGuid().ToString();
                    CookieOptions session = new CookieOptions
                    {
                        Expires = DateTime.Now.AddDays(1)
                    };

                    httpContextAccessor.HttpContext.Response.Cookies.Append("sessionCookie", guid, session);
                    Carts c = new Carts
                    {
                        Guid = guid
                    };
                    if (service.CreateCart(c))
                    {
                        ivm.Guid = guid;
                    }
                }
                else
                {
                    CookieOptions session = new CookieOptions
                    {
                        Expires = DateTime.Now.AddDays(1)
                    };

                    httpContextAccessor.HttpContext.Response.Cookies.Append("sessionCookie", service.GetCartByUserId(int.Parse(userid)).Guid, session);
                }
            }
        }