/// <summary> /// 为cookie添加字典项 /// </summary> /// <param name="cookie"></param> /// <param name="model">对象</param> public static void PutValues <T>(this HttpCookie cookie, T model) where T : class, new() { if (null != cookie) { if (null != model) { var properties = model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); if (properties.Length > 0) { var items = cookie.GetValues(); foreach (var p in properties) { var type = p.PropertyType; if (!type.IsInterface && !type.IsArray && !type.IsGenericType && !type.IsGenericParameter && !type.IsPointer) { var name = p.Name.Trim(); var value = p.GetValue(model); if (null != value) { if (items.Keys.Contains(name)) { cookie.Values.Set(name, value.ToString().Trim()); } else { cookie.Values.Add(name, value.ToString().Trim()); } } } } } } HttpContext.Current.Response.AppendCookie(cookie); } }
public static string GetEx(this HttpCookie httpCookie, string cookieName, string key, bool encrypt) { try { var values = httpCookie.GetValues(cookieName, encrypt); if (values == null) { return(null); } string value = null; if (!string.IsNullOrEmpty(values[key])) { value = values[key]; } return(value); } catch (Exception) { return(null); } }