首先创建ASP.NET CORE Web项目,然后按如下顺序操作。

1.添加nuget程序包:

Microsoft.AspNetCore.Session;
Microsoft.AspNetCore.DataProtection.Redis;
Microsoft.Extensions.Caching.Redis.Core;
Microsoft.Extensions.Caching.Redis
Microsoft.AspNetCore.Http; //使用Session时有扩展方法

2.在appsettings.json中添加Redis配置:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"WebConfig": {
"Redis": {
"Connection": "127.0.0.1:6379,defaultdatabase=1",
//"Connection": "127.0.0.1:6379,allowAdmin=true,password=123456,defaultdatabase=5",
"InstanceName": "Core_Redis_Session_"
},
"SessionTimeOut": "30" //session过期时长,分钟
},
"AllowedHosts": "*"
}

3.在startup.cs类中,按如下例子添加代码:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); /*
* Microsoft.AspNetCore.Session;
* Microsoft.AspNetCore.DataProtection.Redis;
* Microsoft.Extensions.Caching.Redis.Core;
* Microsoft.Extensions.Caching.Redis
* Microsoft.AspNetCore.Http; //使用Session时有扩展方法
*/ #region 使用Redis保存Session
var redisConn = Configuration["WebConfig:Redis:Connection"];
var redisInstanceName = Configuration["WebConfig:Redis:InstanceName"];
//Session 过期时长分钟
var sessionOutTime = Configuration.GetValue<int>("WebConfig:SessionTimeOut", ); //var redis = StackExchange.Redis.ConnectionMultiplexer.Connect(redisConn);
//services.AddDataProtection().PersistKeysToRedis(redis, "DataProtection-Test-Keys");
services.AddDistributedRedisCache(option =>
{
//redis 连接字符串
option.Configuration = redisConn;
//redis 实例名
option.InstanceName = redisInstanceName;
}
);
#endregion //添加Session并设置过期时长
services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(sessionOutTime); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

4.在控制器HomeController中添加:

public class HomeController : Controller
{
public IActionResult Index()
{
ViewData["UserName"] = this.HttpContext.Session.GetString("UserName");
ViewData["PassWord"] = this.HttpContext.Session.GetString("PassWord");
return View();
} [HttpPost]
public NoContentResult Add(string userName, string pwd)
{
this.HttpContext.Session.SetString("UserName", userName); this.HttpContext.Session.SetString("PassWord", pwd); return NoContent();
}
}

5.在View/Index.cshtml添加如下代码:

<form method="post" action="../Home/Add">
<div>
<input name="username" id="username" type="text" value="@ViewData["UserName"]" />
<input name="pwd" id="pwd" type="password" value="" />
<input type="submit" value="更新" />
<h1>提交用户名称为:@ViewData["UserName"] 密码:@ViewData["PassWord"]</h1>
<a href="javascript:void(0);" onclick="window.location.reload();">刷新显示最新值</a>
</div>
</form>

原文链接

.NetCore Session.Redis (转载)的更多相关文章

  1. .NetCore Session.Redis

    首先创建ASP.NET CORE Web项目,然后按如下顺序操作.1.添加nuget程序包: Microsoft.AspNetCore.Session; Microsoft.AspNetCore.Da ...

  2. 在 ASP.NET CORE 中使用 SESSION (转载)

    Session 是保存用户和 Web 应用的会话状态的一种方法,ASP.NET Core 提供了一个用于管理会话状态的中间件.在本文中我将会简单介绍一下 ASP.NET Core 中的 Session ...

  3. Spring boot配合Spring session(redis)遇到的错误

    背景:本MUEAS项目,一开始的时候,是没有引入redis的,考虑到后期性能的问题而引入.之前没有引用redis的时候,用户登录是正常的.但是,在加入redis支持后,登录就出错!错误如下: . __ ...

  4. 单点登录实现(spring session+redis完成session共享)

    一.前言 项目中用到的SSO,使用开源框架cas做的.简单的了解了一下cas,并学习了一下 单点登录的原理,有兴趣的同学也可以学习一下,写个demo玩一玩. 二.工程结构 我模拟了 sso的客户端和s ...

  5. Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享

    小伙伴们好久不见!最近略忙,博客写的有点少,嗯,要加把劲.OK,今天给大家带来一个JavaWeb中常用的架构搭建,即Nginx+Tomcat搭建服务集群,然后通过Spring Session+Redi ...

  6. Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程

    待解决的问题 Spring session(redis存储方式)监听导致创建大量redisMessageListenerContailner-X线程 解决办法 为spring session添加spr ...

  7. SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群

    session集群的解决方案: 1.扩展指定server 利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/ ...

  8. Session Redis Nginx

    Session + Redis + Nginx 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我就简单的介绍一下. Session:在计算机 ...

  9. Spring Boot 应用使用spring session+redis启用分布式session后,如何在配置文件里设置应用的cookiename、session超时时间、redis存储的namespace

    现状 项目在使用Spring Cloud搭建微服务框架,其中分布式session采用spring session+redis 模式 需求 希望可以在配置文件(application.yml)里设置应用 ...

随机推荐

  1. javascript:this指向

    this常见指向问题 this的用法 1.直接在函数中使用 谁调用这个函数this就指向谁 2.对象中使用, 一般情况下指向该对象 3.在构造函数中使用 改变this的指向,两种方法的作用都是相同的, ...

  2. canvas绘画交叉波浪

    做个记录,自己写的动态效果,可能以后用的着呢: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  3. 工作记录(JS向textarea添加固定内容、通过固定字符将字符串分割为数组)

    第一个是在 textarea 输入框中添加固定的内容. 代码如下: <textarea id="text" cols="30" rows="10 ...

  4. webpack4.x初使用

    1.nodejs环境,建议下载v8.2以上版本的,低版本怕是有些api已经废弃,链接附上(https://npm.taobao.org/mirrors/node/) 2.网上一些教程有的是webpac ...

  5. Clumsy 弱网络环境模拟工具使用介绍

    Clumsy 弱网络环境模拟工具使用介绍 by:授客 QQ:1033553122 简介 利用封装 Winodws Filtering Platform 的WinDivert 库, clumsy 能实时 ...

  6. JSTL核心标签库——重定向标签、URL处理标签、网页导入标签

    <c:redirect>重定向标签 相当于HttpServletResponse的sendRedirect()方法. <%@page contentType="text/h ...

  7. 与HttpSession相关的监听器

    概述 与HttpSession相关的监听器有四个:分别是HttpSessionListener.HttpSessionAttributeListener.HttpSessionBindingListe ...

  8. go语言开发环境、goland、IDE

    1.下载: https://studygolang.com/dl 可以从这个网址下载,版本根据你的系统来: 64位系统,可以下载推荐版本: 我安装的是32位系统,下载的是下面这个: 如果你的是其他的系 ...

  9. LeetCode题解之Merge Two Sorted Lists

    1.题目描述 2.题目分析 题目要求合并有序的两个链表,要求不能额外申请空间. 3.代码 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { i ...

  10. VMware部署ubuntu后开机提示piix4_smbus: Host SMBus controller not enabled!

    在虚拟机部署ubuntu10.04-server,每次启动完成之后,出现“piix4_smbus0000:00:07.3: Host SMBus controller not enabled!”提示信 ...