/// <summary> /// Creates web client with the cookies of the given Request /// </summary> /// <param name="CurrentRequest">The current request to copy cookie from</param> /// <param name="stripSession">Strips the ASP.Net_SessionId because by default Sessions have a lock on them, so additional requests during the initial request will cause a timeout. Can override, however must make sure initial request has a read-only session attribute</param> public CookieAwareWebClient(HttpRequest CurrentRequest, bool StripSession = true) { // Copy Cookies CookieContainer cookieJar = new CookieContainer(); // Get list of cookies List <string> CookieKeys = CurrentRequest.Cookies?.AllKeys.ToList(); if (StripSession) { CookieKeys.RemoveAll(x => x.Equals(_SessionIDCookie, StringComparison.InvariantCultureIgnoreCase)); } foreach (string CookieKey in CookieKeys) { if (!StripSession || !CookieKey.Equals(_SessionIDCookie, StringComparison.InvariantCultureIgnoreCase)) { var Cookie = CurrentRequest.Cookies[CookieKey]; // CookieContainer requires a domain. so if it's empty, then use the Current Request's Host. string Domain = string.IsNullOrWhiteSpace(Cookie.Domain) ? CurrentRequest.Url.Host : Cookie.Domain; if (!string.IsNullOrWhiteSpace(Domain)) { cookieJar.Add(new Cookie(CookieKey, Cookie.Value, Cookie.Path, Domain) { Port = CurrentRequest.IsLocal ? $@"""{CurrentRequest.Url.Port}""" : "" }); } } } CookieContainer = cookieJar; RequestUserAgent = CurrentRequest.UserAgent; }
/// <summary> /// Checks if a element is in the cookie based on the key and type /// </summary> /// <typeparam name="T">Type of the element</typeparam> /// <param name="key">CookieKey for lookup</param> /// <returns> /// true if element is found and has the correct type, false if the key doesn´t exists or it has the wrong type /// </returns> public bool HasElement(CookieKey key) { if (this._httpContext.Request.Cookies.AllKeys.Contains(HttpCookieRepository.Name) == false) return false; string value = this._httpContext.Request.Cookies.Get(HttpCookieRepository.Name).Values.Get(key.ToString()); if (value == null) return false; return true; }
/// <summary> /// Get a specific element of the cookie based on the type and key /// </summary> /// <typeparam name="T">Type of the element</typeparam> /// <param name="key">Specific Cookie</param> /// <returns>The element from the cookie</returns> public string GetElement(CookieKey key) { HttpCookie cookie = this._httpContext.Request.Cookies.Get(HttpCookieRepository.Name); if (cookie == null) return null; return cookie.Values.Get(key.ToString()); }
/// <summary> /// Deletes a element in the cookie based on the key /// </summary> /// <param name="key">CookieKey for lookup</param> public void DeleteElement(CookieKey key) { this.TransferCookieFromRequestToResponse(); if (this._httpContext.Response.Cookies.Get(HttpCookieRepository.Name).Values[key.ToString()] != null) { this._httpContext.Response.Cookies.Get(HttpCookieRepository.Name).Values.Remove(key.ToString()); } }
/// <summary> /// Get a specific element of the cookie based on the type and key /// </summary> /// <typeparam name="T">Type of the element</typeparam> /// <param name="key">Specific Cookie</param> /// <returns>The element from the cookie</returns> public string GetElement(CookieKey key) { HttpCookie cookie = this._httpContext.Request.Cookies.Get(HttpCookieRepository.Name); if (cookie == null) { return(null); } return(cookie.Values.Get(key.ToString())); }
/// <summary> /// Adds an element to the cookie. To add several values in an row all elements will be stored in the tmp cookie object at first. /// To avoid "duplicating" the cookie, we remove it from the request. Now we append the cookie (containing the old and the new value) to the /// request again. /// Because we don´t know when the last element is added, we swap the whole request cookiecollection at the end to the response. /// With this way we can add several items and the other cookievalues in the request don´t change. /// </summary> /// <typeparam name="T">Type of the element</typeparam> /// <param name="key">CookieKey for Store</param> /// <param name="data">Data to store</param> public void AddElement(CookieKey key, string data) { HttpCookie cookieInRequest = this.IsCookieInRequest(); if (cookieInRequest == null) { cookieInRequest = new HttpCookie(HttpCookieRepository.Name); this.SetDefaultValues(cookieInRequest); } cookieInRequest.Values.Add(key.ToString(), data); this._httpContext.Request.Cookies.Remove(HttpCookieRepository.Name); this._httpContext.Request.Cookies.Add(cookieInRequest); this.TransferCookieFromRequestToResponse(); }
/// <summary> /// Checks if a element is in the cookie based on the key and type /// </summary> /// <typeparam name="T">Type of the element</typeparam> /// <param name="key">CookieKey for lookup</param> /// <returns> /// true if element is found and has the correct type, false if the key doesn´t exists or it has the wrong type /// </returns> public bool HasElement(CookieKey key) { if (this._httpContext.Request.Cookies.AllKeys.Contains(HttpCookieRepository.Name) == false) { return(false); } string value = this._httpContext.Request.Cookies.Get(HttpCookieRepository.Name).Values.Get(key.ToString()); if (value == null) { return(false); } return(true); }
/// <summary> /// Deletes a element in the cookie based on the key /// </summary> /// <param name="key">CookieKey for lookup</param> public void DeleteElement(CookieKey key) { this.TransferCookieFromRequestToResponse(); if (this._httpContext.Response.Cookies.Get(HttpCookieRepository.Name).Values[key.ToString()] != null) this._httpContext.Response.Cookies.Get(HttpCookieRepository.Name).Values.Remove(key.ToString()); }
/// <summary> /// Updates and element /// </summary> /// <typeparam name="T">Type of the element</typeparam> /// <param name="key">CookieKey for lookup</param> /// <param name="data">New data for storing</param> /// <exception cref="InvalidOperationException">Exception is thrown if element doesn´t exists</exception> public void UpdateElement(CookieKey key, string data) { this.TransferCookieFromRequestToResponse(); this._httpContext.Response.Cookies.Get(HttpCookieRepository.Name).Values[key.ToString()] = data; }
public void Configuration(IAppBuilder app) { //使用Redis共享 var config = ConfigurationManager.GetSection("redisServer") as RedisConfig; var defaultRedis = config.Servers[0]; GlobalHost.DependencyResolver.UseRedis(defaultRedis.IP, defaultRedis.Port, config.Servers.Password, CookieKey.DefaultAppName()); //定制Signalr的UserId var userIdProvider = new MutexConnectionProvider(); GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => userIdProvider); //开启SignalR通道 app.MapSignalR(); }