分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

ASP.NET MVC Redis实现Session

发布时间:2023-09-06 01:06责任编辑:熊小新关键词:.NETMVC

1:Nuget安装StackExchange.Redis

2:Nuget安装RedisSessionStateProvider

在web.config

<sessionState mode="Custom" customProvider="MySessionStateStore"> ?????<providers> ???????<!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki --> ???????<!-- Either use ‘connectionString‘ OR ‘settingsClassName‘ and ‘settingsMethodName‘ OR use ‘host‘,‘port‘,‘accessKey‘,‘ssl‘,‘connectionTimeoutInMilliseconds‘ and ‘operationTimeoutInMilliseconds‘. --> ???????<!-- ‘throwOnError‘,‘retryTimeoutInMilliseconds‘,‘databaseId‘ and ‘applicationName‘ can be used with both options. --> ???????<!-- ?????????<add name="MySessionStateStore" ????????????host = "127.0.0.1" [String] ???????????port = "" [number] ???????????accessKey = "" [String] ???????????ssl = "false" [true|false] ???????????throwOnError = "true" [true|false] ???????????retryTimeoutInMilliseconds = "5000" [number] ???????????databaseId = "0" [number] ???????????applicationName = "" [String] ???????????connectionTimeoutInMilliseconds = "5000" [number] ???????????operationTimeoutInMilliseconds = "1000" [number] ???????????connectionString = "<Valid StackExchange.Redis connection string>" [String] ???????????settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return ‘connectionString‘ value>" [String] ???????????settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of ‘String‘, which is basically ‘connectionString‘ value.>" [String] ???????????loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String] ???????????loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String] ?????????/> ???????--> ???????<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" ?????????????connectionString="127.0.0.1:6379,password=123456,ssl=false,abortConnect=false,connectTimeout=5000" host="" accessKey="" ssl="true" /> ?????</providers> ???</sessionState>

在代码中使用session

 Session["maimai"] = "麦麦";

 Session["haha"] = "哈哈";

redis会生成两个Hash key,如下

{/_jih5op0z3n0pcoo3amv32p3n}_Data

{/_jih5op0z3n0pcoo3amv32p3n}_Internal

Data保存所有的session数据,Internal保存session的过期时间,截图如下

jih5op0z3n0pcoo3amv32p3n 这一串就是我们的sessionId,如下图

再添加一个session测试一下,

 Session["haha"] = "哈哈";

 https://docs.microsoft.com/zh-cn/azure/redis-cache/cache-aspnet-session-state-provider

ASP.NET MVC Redis实现Session

原文地址:http://www.cnblogs.com/baicaocanhua/p/7425543.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved