public static string CookieValue(string cookieString, string cookieName, bool encryptCookie = true) { if (cookieString == null || cookieName == null) return null; string[] tmpData = cookieString.Split('&'); foreach (string item in tmpData) { if (item.StartsWith(cookieName + "=")) { if (encryptCookie) { Crypter crypt = new Crypter(); return crypt.Decrypt(TextHelper.FindP(item + "&", cookieName + "=", "&")); } else return TextHelper.FindP(item + "&", cookieName + "=", "&"); } } return null; }
private static string GetCookieValue(HttpCookie cookie, string cookieValName, bool encryptCookie) { string tmpOut = string.Empty; if (cookie != null) { if (cookie.Values[cookieValName] == null) return tmpOut; try { tmpOut = cookie.Values[cookieValName]; } catch { } if (encryptCookie && !string.IsNullOrWhiteSpace(tmpOut)) { Crypter crypt = new Crypter(); tmpOut = crypt.Decrypt(tmpOut); } return tmpOut; } else return tmpOut; }
private static void ExecuteCreateCookie(HttpRequestBase reqObj, HttpResponseBase respObj, DateTime? expiresOn, string cookieName, string cookieValName, string cookieValue, bool encryptCooke) { HttpCookie cookie = default(HttpCookie); string realCookieValue = cookieValue; if (encryptCooke) { Crypter crypt = new Crypter(); realCookieValue = crypt.Encrypt(cookieValue); crypt = null; } if (reqObj.Cookies[cookieName] == null && respObj.Cookies[cookieName] == null) { cookie = new HttpCookie(cookieName); cookie.Values.Add(cookieValName, realCookieValue); if (expiresOn.HasValue) cookie.Expires = expiresOn.Value; respObj.Cookies.Add(cookie); } else { if (respObj.Cookies[cookieName] != null) cookie = respObj.Cookies[cookieName]; else if (reqObj.Cookies[cookieName] != null) cookie = reqObj.Cookies[cookieName]; try { cookie.Values.Add(cookieValName, realCookieValue); } catch (Exception exc) { throw new Exception("CreateCookie error" + exc.Message); } cookie.Values[cookieValName] = realCookieValue; if (expiresOn.HasValue) { if (cookie.Expires > expiresOn.Value) cookie.Expires = expiresOn.Value; else if (cookie.Expires < DateTime.Now) cookie.Expires = expiresOn.Value; } if (respObj.Cookies[cookieName] != null) respObj.Cookies.Remove(cookie.Name); respObj.Cookies.Add(cookie); } }