• 首先准备redis服务器(docker 和redis3.0内置的哨兵进行高可用设置)
  • 网站配置Redis作为存储session的介质(配置文件这些略)。然后可以了解一下MachineKey这个东西.(MachineKey是用来生成session和解密session的一个xml格式对象)
  • 生成MachineKey
    再startup  config里面配置如下代码
    //抽取key-xxxxx.xml
    services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"D:\"));
    然后再对应的磁盘路径上面找到格式后缀为xml的文件
  • 将文件用记事本打开,然后新增一个类来替换网站默认使用的MachineKey。
    using Microsoft.AspNetCore.DataProtection.Repositories;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq; namespace Session02
    {
    public class CustomXmlRepository : IXmlRepository
    {
    /// <summary>
    /// 设置MachineKey这里的内容就是复制出xml文件的内容
    /// </summary>
    private readonly string keyContent =
    @"<?xml version='1.0' encoding='utf-8'?>
    <key id='6e0d77ae-807d-4dd5-9b33-1f364f6c1f3e' version='1'>
    <creationDate>2018-07-25T07:01:39.5356164Z</creationDate>
    <activationDate>2018-07-25T07:01:39.4800644Z</activationDate>
    <expirationDate>2018-10-23T07:01:39.4800644Z</expirationDate>
    <descriptor deserializerType='Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'>
    <descriptor>
    <encryption algorithm='AES_256_CBC' />
    <validation algorithm='HMACSHA256' />
    <masterKey p4:requiresEncryption='true' xmlns:p4='http://schemas.asp.net/2015/03/dataProtection'>
    <!-- Warning: the key below is in an unencrypted form. -->
    <value>lPUxFutB30oi1KU990Y5nKxeCBnHg7h1JX26nvDlpxdbYciXQr2gdUpLxrL52O/vg8Htrr9F3Xf2fqnVhhAjhw==</value>
    </masterKey>
    </descriptor>
    </descriptor>
    </key>"; public virtual IReadOnlyCollection<XElement> GetAllElements()
    {
    return GetAllElementsCore().ToList().AsReadOnly();
    } private IEnumerable<XElement> GetAllElementsCore()
    {
    yield return XElement.Parse(keyContent);
    }
    public virtual void StoreElement(XElement element, string friendlyName)
    {
    if (element == null)
    {
    throw new ArgumentNullException(nameof(element));
    }
    StoreElementCore(element, friendlyName);
    } private void StoreElementCore(XElement element, string filename)
    {
    }
    }
    }
  • 在startup里面注入CustomXmlRepository使用默认的key来生成session
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.DataProtection.Repositories;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection; namespace Session02
    {
    public class Startup
    {
    public Startup(IConfiguration configuration)
    {
    Configuration = configuration;
    } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
    //抽取key-xxxxx.xml
    //services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"D:\"));
    services.AddSingleton<IXmlRepository, CustomXmlRepository>();
    services.AddDataProtection(configure =>
    {
    configure.ApplicationDiscriminator = "Seesion.testweb";
    });
    services.AddDistributedRedisCache(option =>
    {
    //redis 数据库连接字符串
    option.Configuration = Configuration.GetConnectionString("RedisConnection");
    //redis 实例名
    option.InstanceName = "test";
    });
    services.AddSession();
    services.AddMvc();
    } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    app.UseSession();
    app.UseMvc();
    }
    }
    }
  • 多个网站都用以上方法进行配置,然后测试一波

netcore 使用redis session 分布式共享的更多相关文章

  1. nginx+iis+redis+Task.MainForm构建分布式架构 之 (redis存储分布式共享的session及共享session运作流程)

    本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,上一篇分享文章制作是在windows上使用的nginx,一般正式发布的时候是在linux来配 ...

  2. Session分布式共享 = Session + Redis + Nginx

    一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我就简单的介绍一下. Session:在计算机中,尤其是在网络应用中,称为"会话控制 ...

  3. Session分布式共享 = Session + Redis + Nginx(转)

    出处:http://www.cnblogs.com/newP/p/6518918.html 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我 ...

  4. 详解Session分布式共享(.NET CORE版)

    一.前言&回顾 在上篇文章Session分布式共享 = Session + Redis + Nginx中,好多同学留言问了我好多问题,其中印象深刻的有:nginx挂了怎么办?采用Redis的S ...

  5. 什么是Session分布式共享

    在了解session分布式共享之前先来了解Session.Redis和Nginx的相关知识. 一.Session相关知识 1.Session 介绍 Session在网络应用中,称为“会话控制”. 每个 ...

  6. asp.net session分布式共享解决方案

    Session共享是分布式系统设计时必须考虑的一个重要的点.相比较java中的session共享解决方案,.net中的解决方案还是比较少,MemcachedSessionProvider类库是比较优秀 ...

  7. 【Redis】分布式Session

    一.问题引出 1.1 Session的原理 1.2 问题概述 二.解决方案 三.代码实现-使用Token代替Session 3.1 Service 3.2 TokenController 一.问题引出 ...

  8. EhCache+Redis实现分布式缓存

    Ehcache集群模式 由于 EhCache 是进程中的缓存系统,一旦将应用部署在集群环境中,每一个节点维护各自的缓存数据,当某个节点对缓存数据进行更新,这些更新的数据无法在其它节点中共享,这不仅会降 ...

  9. 使用Spring Session和Redis解决分布式Session跨域共享问题

    http://blog.csdn.net/xlgen157387/article/details/57406162 使用Spring Session和Redis解决分布式Session跨域共享问题

随机推荐

  1. AI换脸教程:DeepFaceLab使用教程(1.安装及分解视频)

    首先需要选择合适的DeepFaceLab下载(https://www.deepfacelabs.com/list-5-1.html),然后安装相应的显卡驱动,如果已经准备好这些工作,那么恭喜你,终于开 ...

  2. JAVA并发编程的艺术 JMM内存模型

    锁的升级和对比 java1.6为了减少获得锁和释放锁带来的性能消耗,引入了"偏向锁"和"轻量级锁". 偏向锁 偏向锁为了解决大部分情况下只有一个线程持有锁的情况 ...

  3. Mysql定时器定时删除表数据

    由于测试环境有张日志表没定时2分钟程序就狂插数据,导致不到1一个月时间,这张日志表就占用了6.7G的空间,但是日志刷新较快,有些日志就没什么作用,就写了个定时器,定期删除这张表的数据 首先先查看mys ...

  4. event loop 与 vue

    结论 对于event loop 可以抽象成一段简单的代码表示 for (macroTask of macroTaskQueue) { // 1. Handle current MACRO-TASK h ...

  5. HDU 2923 Relocation(状压dp+01背包)

    题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...

  6. python 不灭

    进程与线程的区别? 1进程是CPU资源分配的最小单元,线程是CPU计算的最小单元. 2一个进程中可以有多个线程 3对于python来说,它的进程与线程与其它语言有差异,它是有GIL锁,保证同一进程中, ...

  7. 图的普里姆(Prim)算法求最小生成树

    关于图的最小生成树算法------普里姆算法 首先我们先初始化一张图: 设置两个数据结构来分别代表我们需要存储的数据: lowcost[i]:表示以i为终点的边的最小权值,当lowcost[i]=0说 ...

  8. 微信小程序的 音频 组件

    audio:音频组件, api 接口为 wx.createInnerAudioContext audio 组件的属性: id:类型 字符串 audio 组件的唯一标识 src:类型 字符串 要播放音频 ...

  9. 安装 Windows 系统在 NVMe 规范的 M.2 接口的固态硬盘(SSD)上

    作为一个程序员很重要的一项技能就是装系统 @_@,以前我都是随便用网上的工具做个系统盘,每次要用直接随手就搞好了,节省大家时间. 但最近同事装了个贼小的固态,然后我启动盘里的系统果断识别不出来他的固态 ...

  10. handsonetable+vue 表格在线编辑

    <template> <div> <div id="example-container" class="wrapper"> ...