①添加nuget程序包:
"ConnectionRedis": { "Connection": "12⑦0.0.1:6379,allowAdmin=true,password=123456,defaultdatabase=0", "InstanceName": "SukCore_Redis_Session_", "SessionTimeOut": "20" },
public IServiceProvider ConfigureServices(IServiceCollection services) { services.Configure(options => options.CheckConsentNeeded = context => false; //这里要改为false,默认是true,true的时候session无效 }); ConnectionConfigModel con = ConfigurationManager.GetAppSettings (); #endregion }
Configure 中 添加?app.UseSession();?
注意? ?app.UseSession();?一定要在?app.UseMvc 之前?
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //使用session app.UseSession(); app.UseMvc(routes => name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
public class HomeController : Controller { public NoContentResult Add(string userName,string pwd) this.HttpContext.Session.SetString("UserName", userName); this.HttpContext.Session.SetString("PassWord", pwd); ViewData["UserName"] = this.HttpContext.Session.GetString("UserName"); ViewData["PassWord"] = this.HttpContext.Session.GetString("PassWord"); return NoContent(); }
在调试的过程中遇到一个尴尬的问题? controller中打断点? ?没有执行session之前 先查看了 session id 发现一直为空? 还以为错了? 结果最后发现 要先执行代码不能先查看
附上我的错误截图 看没有和我一样傻的 哈哈哈...
错误的操作
正确的操作?
以上就是土嘎嘎小编为大家整理的ASP.NET_Core_使用Redis_存储Session_实现共享_Session相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!