Net Core using Redis save-process session

Redsi Download: https://github.com/MSOpenTech/redis/releases .

After the download, unzip

Find redis.windows.conf file ( modify the password that step can be omitted )

 

Open the find requirepass node, open the comment symbol "#", change the value back to set up their own password

 

Open cmd window, cd to the directory to unzip the program directory or directory directly into the address bar extracting file input cmd carriage return  

redis-server.exe redis.windows.conf

 

New start a cmd window  

redis-cli.exe -h 127.0.0.1 -p 6379 -a Mypwd

 

If the password is not set execution

redis-cli.exe -h 127.0.0.1 -p 6379

Then set get access execution value

 

VS operation

Add StackExchange.Redis package project

Install-Package StackExchange.Redis

Profile Configuration link Redis string

{
  "ConnectionStrings": {
    "RedisConn": "127.0.0.1:6379,allowAdmin=true,password=Mypwd,defaultdatabase=0"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "COREHOST_TRACE": 1,
  "AllowedHosts": "*"
}

Startup.cs 里ConfigureServices方法添加 session 及Redis设置

            services.AddDistributedRedisCache(options =>
            {
                options.Configuration = Configuration.GetConnectionString("RedisConn");
                options.InstanceName = "MyTest.Web_";
            });
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false; // Default is true, make it false
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            //添加session
            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromHours(2); //session 有效期
                options.Cookie.HttpOnly = true;//设为httponly
            });

Startup.cs 里Configuref方法添加 

 

app.UseSession();

 程序使用session既可

//写入
HttpContext.Session.SetString("testkey", "abc");
//读取
string uid = HttpContext.Session.GetString("mruserid");

View management tool with Redis Redis Desktop Manager link

  

May be displayed on a portion of the FIG incomplete fill a map 

 

Guess you like

Origin www.cnblogs.com/dwtx/p/11094260.html