1:nuget installation Stackexchange.redis
2:nuget installation Redissessionstateprovider
In the Web. config
<sessionstateMode= "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 is 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 = "DatabaseId" [number] = "0" [number] ApplicationName = "" [String] ] Connectiontimeoutinmilliseconds = "Operationtimeoutinmilliseconds" [number] = "+" [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 has a return type of ' String ', which is basically ' C Onnectionstring ' value.> ' [StRing] Loggingclassname = "<assembly qualified class name that contains logging method specified below>" [ String] Loggingmethodname = "<logging method should is defined in Loggingclass. It should is public, static, does not take any parameters and should has a return type of system.io.textwriter.> "[Str ing]/> - <Addname= "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>
Using the session in your code
session["Maimai"] = "Mai Mai";
session["haha"] = "haha";
Redis generates two hash keys, as follows
{/_jih5op0z3n0pcoo3amv32p3n}_data
{/_jih5op0z3n0pcoo3amv32p3n}_internal
Data to save all sessions, internal save the session expiration time, as follows
Jih5op0z3n0pcoo3amv32p3n This string is our SessionID, such as
Add a session test again,
session["haha"] = "haha";
Https://docs.microsoft.com/zh-cn/azure/redis-cache/cache-aspnet-session-state-provider
ASP. NET MVC Redis Implementation session