ASP. NET MVC Redis Implementation session

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.