Login
网站首页 > 文章中心 > 其它

ASP.NET_Core_使用Redis_存储Session_实现共享_Session

作者:小编 更新时间:2023-10-07 10:44:41 浏览量:311人看过

①添加nuget程序包:

ASP.NET_Core_使用Redis_存储Session_实现共享_Session-图1


"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

正确的操作?

ASP.NET_Core_使用Redis_存储Session_实现共享_Session

以上就是土嘎嘎小编为大家整理的ASP.NET_Core_使用Redis_存储Session_实现共享_Session相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章