Search code examples
c#asp.net-corecookies

Accessing Cookies from static class in ASP.net Core


I'm Trying to access Cookies from Static Class in Asp.net Core , So far I couldn't find any useful topics on Google. any Suggestions?

I already Did that in asp.net:

public static string GetString(string Key)
        {
            var format = System.Web.HttpContext.Current.Request.Cookies["Language"];
            if (format == null)
                format = new HttpCookie("Language") { Value = "Fa" };
            ResourceManager rm = new ResourceManager("Ippv.Virtual.Application.Properties." + format.Value,
                Assembly.GetExecutingAssembly());
            return rm.GetString(Key);
        }

How can I write equivalent of this code in asp.net core?


Solution

  • In ASP.NET, we can access cookies using httpcontext.current but in ASP.NET Core, there is no htttpcontext.currently. In ASP.NET Core, everything is decoupled and modular.Httpcontext is accessible from Request object and the IHttpContextAccessor interface which is under "Microsoft.AspNetCore.Http" namespace and this is available anywhere in the application. Refer to the following code:

     public class TestController : Controller
        {
            private readonly IHttpContextAccessor _httpContextAccessor;
    
            public TestController(IHttpContextAccessor httpContextAccessor)
            {
                this._httpContextAccessor = httpContextAccessor;
            }
            public  IActionResult Index()
            {
     //read cookie from IHttpContextAccessor  
                string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];
                //read cookie from Request object  
                string cookieValueFromReq = Request.Cookies["Key"];
                //set the key value in Cookie  
                Set("kay", "Hello from cookie", 10);
                //Delete the cookie object  
                Remove("Key");
                return View();
            }
            /// <summary>  
            /// Get the cookie  
            /// </summary>  
            /// <param name="key">Key </param>  
            /// <returns>string value</returns>  
            public string Get(string key)
            {
                return Request.Cookies[key];
            }
            /// <summary>  
            /// set the cookie  
            /// </summary>  
            /// <param name="key">key (unique indentifier)</param>  
            /// <param name="value">value to store in cookie object</param>  
            /// <param name="expireTime">expiration time</param>  
            public void Set(string key, string value, int? expireTime)
            {
                CookieOptions option = new CookieOptions();
                if (expireTime.HasValue)
                    option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
                else
                    option.Expires = DateTime.Now.AddMilliseconds(10);
                Response.Cookies.Append(key, value, option);
            }
            /// <summary>  
            /// Delete the key  
            /// </summary>  
            /// <param name="key">Key</param>  
            public void Remove(string key)
            {
                Response.Cookies.Delete(key);
            }