I am currently working on a website with a "shopping cart", when you open the shopping cart page, it creates a cookie with an ID, then this user is "registered" into database as if they signed up, but lacks some information like name, a proper username (instead adds a GUID) etc. so when they sign up, instead of creating a new user, this user in database gets updated and everything in shopping cart is already linked to this new account.
Problem with this is, this ID is only assigned if and only if user enters the link where shopping cart is, not any other page of website.
I can simply add this cookie creating to every single page in the website but instead of that, is there a way to make it work in any page of the website?
Below is a sample from function in Controller I use to create a cookie.
HttpCookie cookie = Request.Cookies.Get("UserId");
if (cookie == null)
{
string cookieValue = Guid.NewGuid().ToString();
Customer customer = new Customer();
customer.UserName = cookieValue;
if (SqlQuery.InsertGuest(customer))
{
HttpCookie userIdCookie = new HttpCookie("userId");
userIdCookie.Value = cookieValue;
userIdCookie.Expires = DateTime.Now.AddDays(90);
Response.SetCookie(userIdCookie);
}
}
Do I have to add this to every page controller of the website? Or is there an alternative?