netcore转java,.NetCore Session.Redis (转载)

首先创建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("WebConfig:SessionTimeOut", 30);

//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添加如下代码:

提交用户名称为:@ViewData["UserName"] 密码:@ViewData["PassWord"]

刷新显示最新值

https://www.cnblogs.com/OpenCoder/category/1132736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值