Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IHttpContextAccessor in static class to set cookies

I am trying to create a generic addReplaceCookie method in a static class. The method would look something like this

public static void addReplaceCookie(string cookieName, string cookieValue)
{

    if ((HttpContext.Current.Request.Cookies(cookieName) == null))
    {
        // add cookie
        HttpCookie s = new HttpCookie(cookieName);
        s.Value = cookieValue;
        s.Expires = DateTime.Now.AddDays(7);
        HttpContext.Current.Response.Cookies.Add(s);
    }
    else {
        // ensure cookie value is correct 
        HttpCookie existingSchoolCookie = HttpContext.Current.Request.Cookies(cookieName);
        existingSchoolCookie.Expires = DateTime.Now.AddDays(7);
        existingSchoolCookie.Value = cookieValue;
        HttpContext.Current.Response.Cookies.Set(existingSchoolCookie);
    }

}

I know that in order to get the HttpContext in asp.net core you have to use the IHttpContextAccessor. But I cannot inject it into a static class.

Is there another way to get access to it?

I am using rc1-final.

like image 749
user973671 Avatar asked May 19 '16 16:05

user973671


People also ask

How to use cookies in MVC with ihttpcontextaccessor?

In ASP.NET MVC, we accessed cookies from httpcontext but in .NET Core, we need to use IHttpContextAccessor interface which falls under “Microsoft.AspNetCore.Http” namespace Now, We can use the cookies by following the below codes. First, we need to add an IHttpContextAccessor in the ConfigureServices method of Startup class.

When do I need to use ihttpcontextaccessor?

It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service. The Razor Pages PageModel exposes the HttpContext property:

How do I use httpcontext in Blazor?

Blazor and shared state ASP.NET Core apps access HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service. Use HttpContext from Razor Pages

How do I access httpcontext in ASP NET Core?

ASP.NET Core apps access HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service. The Razor Pages PageModel exposes the HttpContext property:


2 Answers

While i would advise staying away from static class scenarios like this, it is still possible to achieve what you are asking for.

Assuming a static class like...

public class MyStaticHelperClass {
    private static IHttpContextAccessor httpContextAccessor;
    public static void SetHttpContextAccessor(IHttpContextAccessor  accessor) {
        httpContextAccessor = accessor;
    }

    public static void addReplaceCookie(string cookieName, string cookieValue) {
        var HttpContext = httpContextAccessor.HttpContext;
        if (HttpContext.Request.Cookies(cookieName) == null) {
            // add cookie
            HttpCookie s = new HttpCookie(cookieName);
            s.Value = cookieValue;
            s.Expires = DateTime.Now.AddDays(7);
            HttpContext.Response.Cookies.Add(s);
        } else {
            // ensure cookie value is correct 
            HttpCookie existingSchoolCookie = HttpContext.Request.Cookies(cookieName);
            existingSchoolCookie.Expires = DateTime.Now.AddDays(7);
            existingSchoolCookie.Value = cookieValue;
            HttpContext.Response.Cookies.Set(existingSchoolCookie);
        }
    }
}

You would add the accessor in Startup.ConfigureServices since it is no longer added automatically

public void ConfigureServices(IServiceCollection service) {

    //Register IHttpContextAccessor and its implementation.
    services.AddHttpContextAccessor();

    services.AddTransient<IMyService, MyService>();
    services.AddMvc();

    //...
}

And get the service via injection into the Startup.Configure method

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpContextAccessor accessor)
{
    MyStaticHelperClass.SetHttpContextAccessor(accessor);

    //...

}

Now with that done. I would still strongly advise converting your static class into a service whose concrete implementation would use the IHttpContextAccessor as a dependency that can be injected via its constructor.

public interface ICookieService {
    void AddReplaceCookie(string cookieName, string cookieValue);
}

public class CookieService : ICookieService {
    IHttpContextAccessor httpContextAccessor;
    public CookieService(IHttpContextAccessor httpContextAccessor) {
        this.httpContextAccessor = httpContextAccessor;
    }
    public void AddReplaceCookie(string cookieName, string cookieValue) {
        var HttpContext = httpContextAccessor.HttpContext;
        if (HttpContext.Request.Cookies(cookieName) == null) {
            // add cookie
            HttpCookie s = new HttpCookie(cookieName);
            s.Value = cookieValue;
            s.Expires = DateTime.Now.AddDays(7);
            HttpContext.Response.Cookies.Add(s);
        } else {
            // ensure cookie value is correct 
            HttpCookie existingSchoolCookie = HttpContext.Request.Cookies(cookieName);
            existingSchoolCookie.Expires = DateTime.Now.AddDays(7);
            existingSchoolCookie.Value = cookieValue;
            HttpContext.Response.Cookies.Set(existingSchoolCookie);
        }
    }
}

...that could then be registered with the Services collection...

public void ConfigureServices(IServiceCollection service) {

    services.AddHttpContextAccessor();

    services.AddTransient<ICookieService, CookieService>();
    services.AddMvc();
}

...and be available for injection into classes that have need of it's use.

public class SomeClassThatNeedCookieServicesController : Controller {
    ICookieService cookieService;

    public SomeClassThatNeedCookieServicesController(ICookieService cookieService) {
        this.cookieService = cookieService;
    }

    //...
}

This is how I do it to manage session cookies in my applications.

like image 107
Nkosi Avatar answered Oct 28 '22 04:10

Nkosi


in Startup.ConfigureServices:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

later in Startup.Configure add by DI IServiceProvider and use it to extract IHttpContextAccessor like this:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
    {
        IHttpContextAccessor accessor = svp.GetService<IHttpContextAccessor>();
        MyRepository.SetHttpContextAccessor(accessor);
like image 28
EladTal Avatar answered Oct 28 '22 04:10

EladTal