62,046
社区成员
发帖
与我相关
我的任务
分享
public static String CookieName = "SHOPPING_INFO";
/// <summary>
/// 添加商品进购物车
/// </summary>
/// <param name="productId"></param>
/// <param name="num"></param>
/// <returns></returns>
public static bool AddProduct(int productId, int num) {
HttpCookie hc = null;
String oldCookieValue;
String NewValue;
hc = HttpContext.Current.Request.Cookies[CookieName];
if (hc == null) //如果Cookie不存在 则直接新建Cookie 保存商品id和数量;
{
hc = new HttpCookie(CookieName);
hc.Expires = DateTime.Now.AddDays(7);
hc.Value = productId.ToString() + "|" + num.ToString();
}
else //假设Cookie存在 则处理
{
oldCookieValue = hc.Value;
NewValue = AddCookieValue(oldCookieValue, productId, num);
hc.Value = NewValue;
}
HttpContext.Current.Response.AppendCookie(hc);
//Cookie写入Response
return true; //结束
}
/// <summary>
/// 获取购物车信息
/// </summary>
/// <returns></returns>
public static string GetShoppingInfo() {
HttpCookie hc = null;
String oldCookieValue;
hc = HttpContext.Current.Request.Cookies[CookieName];
if (hc == null) //如果Cookie存在 则直接新建Cookie 保存商品id和数量;
{
return null;
}
else {
return hc.Value;
}
}
//验证用户名密码
if (new UsersDAL().IsLogin(this.txtName.Value, this.txtPwd.Value)) {
Users userEntity = new UsersDAL().GetEntity(this.txtName.Value);
string status = userEntity.Status.ToString();//获取登录用户的权限
string userId = userEntity.UserID.ToString();//获取用户ID
bool save = IsSave.Checked == true ? true : false;
Authentication.SetCookie(this.txtName.Value, this.txtPwd.Value, status, userId, save);//填入cookie
}
public static void SetCookie(string UserName, string PassWord, string Rights, string usrId, bool save) {
String UserData = UserName + "#" + PassWord + "#" + Rights + "#" + usrId;
//数据放入ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(60), save, UserData);
//数据加密
string enyTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
HttpContext.Current.Response.Cookies.Add(cookie);
}