[转]Asp.net Core中使用Session
本文转自:http://www.cnblogs.com/sword-successful/p/6243841.html
前言
2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年。
元旦放假在家写了个Asp.net Core验证码登录, 做demo的过程中遇到两个小问题,第一是在Asp.net Core中引用dll,以往我们引用DLL都是直接引用,在Core里这样是不行的,必须基于NuGet添加,或者基于project.json添加,然后保存VS会启动还原类库。
第二就是使用Session的问题,Core里使用Session需要添加Session类库。
添加Session
在你的项目上基于NuGet添加:Microsoft.AspNetCore.Session。
修改startup.cs
在startup.cs找到方法ConfigureServices(IServiceCollection services) 注入Session(这个地方是Asp.net Core pipeline):services.AddSession();
接下来我们要告诉Asp.net Core使用内存存储Session数据,在Configure(IApplicationBuilder app,...)中添加代码:app.UserSession();
Session
1、在MVC Controller里使用HttpContext.Session
using Microsoft.AspNetCore.Http; public class HomeController:Controller
{
public IActionResult Index()
{
HttpContext.Session.SetString("code","123456");
return View();
} public IActionResult About()
{
ViewBag.Code=HttpContext.Session.GetString("code");
return View();
}
}
2、如果不是在Controller里,你可以注入IHttpContextAccessor
public class SomeOtherClass
{
private readonly IHttpContextAccessor _httpContextAccessor;
private ISession _session=> _httpContextAccessor.HttpContext.Session; public SomeOtherClass(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor=httpContextAccessor;
} public void Set()
{
_session.SetString("code","123456");
} public void Get()
{
string code = _session.GetString("code");
}
}
存储复杂对象
存储对象时把对象序列化成一个json字符串存储。
public static class SessionExtensions
{
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
} public static T GetObjectFromJson<T>(this ISession session, string key)
{
var value = session.GetString(key); return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
var myComplexObject = new MyClass();
HttpContext.Session.SetObjectAsJson("Test", myComplexObject); var myComplexObject = HttpContext.Session.GetObjectFromJson<MyClass>("Test");
使用SQL Server或Redis存储
1、SQL Server
添加引用 "Microsoft.Extensions.Caching.SqlServer": "1.0.0"
注入:
// Microsoft SQL Server implementation of IDistributedCache.
// Note that this would require setting up the session state database.
services.AddSqlServerCache(o =>
{
o.ConnectionString = "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
o.SchemaName = "dbo";
o.TableName = "Sessions";
});
2、Redis
添加引用 "Microsoft.Extensions.Caching.Redis": "1.0.0"
注入:
// Redis implementation of IDistributedCache.
// This will override any previously registered IDistributedCache service.
services.AddSingleton<IDistributedCache, RedisCache>();
参考
http://benjii.me/2016/07/using-sessions-and-httpcontext-in-aspnetcore-and-mvc-core/
博客地址: | http://www.cnblogs.com/sword-successful/ |
博客版权: | 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。 如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。 |
最新文章
- ★Kali信息收集~★7.FPing :ip段扫描
- 获得APP当前显示的viewController
- 如何用ZBrush做人体造型雕刻
- win10自动更新彻底关闭
- [MODx] 7. MIGX DB
- Android 应用程序启动过程源代码分析
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(13)-权限设计
- jquery禁用右键单击、F5刷新
- 【C# in depth 第三版】温故而知新(1)
- go接口
- Vim 入门:基础
- 牛客练习赛22 简单瞎搞题(bitset优化dp)
- 新网站如何做SEO优化【转】
- [转]asp.net core中的View Component
- run commands in linux shell using batch file
- MySql_34道经典Sql试题
- TCP/IP四层协议模型与ISO七层模型
- ChemDraw怎么绘制H-点或H-划
- webpack无法通过 IP 地址访问 localhost 解决方案
- iOS企业开发In House ipa发布流程
热门文章
- CentOS6.5上Zabbix3.0的RPM安装【一】-安装并配置Server
- day05.1-文件归档与压缩
- Unity自带IAP插件使用(googleplay)
- [SinGuLaRiTy] (树形)数据结构题目复习
- 安装Scrapy报错 error: Microsoft Visual C++ 14.0 is required解决方法
- Common operators to overload-c++运算符重载的标准语法(全)
- linux惊群
- git 本地分支与远程分支相关操作记录
- C++在WINdow桌面绘制文字图形
- 本地命令上传文件到服务器以及linux编辑过程中非正常退出问题