I have seen this code in ShoppingCart class in Music Store MVC3 tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store
// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
Why is GUID needed? I am asking for somebody to explain what is it used for in this example.
Have a look at part 8 of that tutorial, under the heading “Managing the Shopping Cart business logic” – this explains the purpose of that code.
Basically, the GUID is used to uniquely identify a user, without forcing them to have logged in, so that the application can keep track of items that they have put into the shopping cart.