ورود

View Full Version : session cookies



mona11
دوشنبه 30 تیر 1393, 18:45 عصر
سلام.میخواستم توضیحاتی در مورد 3 تابع زیر بدونم که چه کاری انجام میدن.


// 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();
}

public static ShoppingCart GetCart(HttpContextBase context)
{
var cart = new ShoppingCart();
cart.ShoppingCartId = cart.GetCartId(context);
return cart;
}

// Helper method to simplify shopping cart calls
public static ShoppingCart GetCart(Controller controller)
{
return GetCart(controller.HttpContext);
}

mona11
دوشنبه 30 تیر 1393, 18:54 عصر
آها فهمیدم.تابع اول میگه که اگر کاربر لاگین کرده،مقدار سشن رو برابر نام کاربری اون فرد بزار و اگر کاربر لاگین نکرده،یک Guid از سیستم بگیر و از اون استفاده کن.

در واقع این شرط یعنی اینکه کاربر لاگین کرده یا خیر

if (!string.IsNullOrWhiteSpace(context.User.Identity. Name))