[转]How to use IHttpContextAccessor in static class to set cookies
While i would advise staying away from static class scenarios like this, it is still possible to achieve what you are asking for.
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;
}
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();
}
...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.
最新文章
- Android中ListView控件的使用
- History(历史)命令用法
- 51nod1693 水群
- Jena语义Web开发101
- SQL Server获取下一个编码字符实现
- linux安装php出现的各种错误解决方案
- hibernate spring sturts2配置
- 三、C# 运算符和控制流
- PHP中字符串类型与数值类型混合计算
- 5.4.2 RegExp实例方法
- laravel 报错 mcrypt_decrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported
- TSQL编程
- Jquery实现数据双向绑定(赋值和取值),类似AngularJS
- 家庭记账本小程序之删(java web基础版四)
- oracle11在docker环境下的运行
- sql server 压缩备份数据库
- sklearn中的数据集的划分
- Browsersync结合gulp和nodemon实现express全栈自动刷新
- Aptana在Eclipse的安装
- mysql 数据库备份的多种方式
热门文章
- 在win7系统上搭建django+oracle 11g时,注意事项
- django日期查询出现UTC日志转换CONVERT_TZ出错的问题
- Ajax轮询 select循环输出
- 安装opencv-python
- Optimizing and caching browser bundles...
- google 浏览器的Debug 调试工具使用
- ICEM二维网格
- 【2-SAT】【并查集】NOIp模拟题 植树方案 题解
- 栈和递归的关系 144:Binary Tree Preorder Traversal
- LightOJ - 1032 数位DP