.Netcore使用Session


1.使用Session(進程內)

在startup中添加方法 services.AddSession  app.UseSession()

 services.AddDistributedMemoryCache();

2.使用Redis存儲Session(單節點)

services.AddDistributedRedisCache(option => {

 option.Configuration = “127.0.0.1:6379”;
 option.InstanceName = 30;
});

3.使用Redis分布式存儲 (分布式)

nuget:     Microsoft.AspNetCore.DataProtection

services.AddDataProtection()
.SetApplicationName(Configuration["Redis:Session_application_name"])
.PersistKeysToRedis(ConnectionMultiplexer.Connect(redisconfig), "DataProtection-Keys");

示例:

使用方式: 

 HttpContext.Session.SetString("key", “value”);  //設置

HttpContext.Session.GetString("key");//獲取session

4.使用Sqlserver數據庫存儲Session

services.AddDistributedSqlServerCache

 

參考:

ASP.NET Core中間件實現分布式 Session: https://www.cnblogs.com/vipyoumay/p/7771237.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM