redis 分布式缓存session 方式登录

  使用 步骤

1 nuget Microsoft.Extensions.Caching.Redis

2注册服务+注入

services.AddSession() ; 

services.AddDistributedRedisCache (options =>
{
options.Configuration = "127.0.0.1:6379";
options. InstanceName = "RedisDistributedCache";

});

app.useSession()
3启动Redis-配置连接
4 Session共享---分布式缓存

页面测试:

public IActionResult Session (
string result = base.HttpContext.Session.GetString("currentUser");

if (string.IsNull0rWhiteSpace(result))
base.HttpContext.Session.SetString("currentUser","jason") ;

return view () ;

}

本质上还是利用ioc  扩展session 的 缓存机制

public static IServiceCollection AddSession(this IServiceCollection services){
if (services == null)
{
throw new ArgumentNullException(nameof(services)) ;
services.TryAddTransientK<ISessionStore,DistributedSessionStore>() ;
services.AddDataProtection( ;
return services;
}

DistributedSessionStore  实现了IdistributedCache


 
原文地址:https://www.cnblogs.com/jasontarry/p/15380618.html