如何使用IHttpContextAccessor在静态类设置Cookie [英] How to use IHttpContextAccessor in static class to set cookies

查看:3364
本文介绍了如何使用IHttpContextAccessor在静态类设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个静态类的通用 addReplaceCookie 方法。该方法会是这个样子。

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);
    }

}



我知道,为了得到在的HttpContext 在asp.net中的核心,你必须使用 IHttpContextAccessor 。但我不能把它注入到一个静态类。

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?

我使用RC1决赛。

推荐答案

虽然我会建议住从静态类的场景是这样了,它仍然是可能的达到你所要求的东西。

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

Startup.ConfigureServices 方法,你可以调用 services.BuildServiceProvider()来获得的IServiceProvider 来解决你所寻求的类型。这是一个黑客位,但它的工作原理。

In the Startup.ConfigureServices method you can call services.BuildServiceProvider() to get the IServiceProvider to resolve the type you seek. It's a bit of a hack but it works.

假设一个静态类像...

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 configure your class during start up

public IServiceProvider ConfigureServices(IServiceCollection service) {
    services.AddTransient<IMyService, MyService>();
    services.AddMvc();

    //this would have been done by the framework any way after this method call;
    //in this case you call the BuildServiceProvider manually to be able to use it
    var serviceProvider = services.BuildServiceProvider();

    //here is where you set you accessor
    var accessor = serviceProvider.GetService<IHttpContextAccessor>()
    MyStaticHelperClass.SetHttpContextAccessor(accessor);

    return serviceProvider;
}

现在与完成。我仍然强烈建议您静态类转换成其具体实施将使用 IHttpContextAccessor 作为可以通过其构造函数注入依赖的服务。

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.AddTransient<ICookieService, CookieService>();
    services.AddMvc();
}



...并可供注入到那些需要的IT类的使用

...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;
    }
}

这是我做它来管理会话cookie中我的应用程序。

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

这篇关于如何使用IHttpContextAccessor在静态类设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆