本文转自:http://stackoverflow.com/questions/37329354/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.

最新文章

  1. Android中ListView控件的使用
  2. History(历史)命令用法
  3. 51nod1693 水群
  4. Jena语义Web开发101
  5. SQL Server获取下一个编码字符实现
  6. linux安装php出现的各种错误解决方案
  7. hibernate spring sturts2配置
  8. 三、C# 运算符和控制流
  9. PHP中字符串类型与数值类型混合计算
  10. 5.4.2 RegExp实例方法
  11. laravel 报错 mcrypt_decrypt(): Key of size 11 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported
  12. TSQL编程
  13. Jquery实现数据双向绑定(赋值和取值),类似AngularJS
  14. 家庭记账本小程序之删(java web基础版四)
  15. oracle11在docker环境下的运行
  16. sql server 压缩备份数据库
  17. sklearn中的数据集的划分
  18. Browsersync结合gulp和nodemon实现express全栈自动刷新
  19. Aptana在Eclipse的安装
  20. mysql 数据库备份的多种方式

热门文章

  1. 在win7系统上搭建django+oracle 11g时,注意事项
  2. django日期查询出现UTC日志转换CONVERT_TZ出错的问题
  3. Ajax轮询 select循环输出
  4. 安装opencv-python
  5. Optimizing and caching browser bundles...
  6. google 浏览器的Debug 调试工具使用
  7. ICEM二维网格
  8. 【2-SAT】【并查集】NOIp模拟题 植树方案 题解
  9. 栈和递归的关系 144:Binary Tree Preorder Traversal
  10. LightOJ - 1032 数位DP