如何在静态类中使用 IHttpContextAccessor 来设置 cookie [英] How to use IHttpContextAccessor in static class to set cookies
问题描述
我正在尝试在静态类中创建一个通用的 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);
}
}
我知道为了在 asp.net 核心中获得 HttpContext
,你必须使用 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-final.
I am using rc1-final.
推荐答案
虽然我建议远离像这样的静态类场景,但仍有可能实现您的要求.
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);
}
}
}
您将在 Startup.ConfigureServices
中添加访问器,因为它不再自动添加
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();
//...
}
并通过注入Startup.Configure
方法获取服务
And get the service via injection into the Startup.Configure
method
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IHttpContextAccessor accessor)
{
MyStaticHelperClass.SetHttpContextAccessor(accessor);
//...
}
现在完成了.我仍然强烈建议将您的静态类转换为服务,其具体实现将使用 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.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;
}
//...
}
这就是我在应用程序中管理会话 cookie 的方式.
This is how I do it to manage session cookies in my applications.
这篇关于如何在静态类中使用 IHttpContextAccessor 来设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!