首頁  >  文章  >  後端開發  >  解析Asp.net如何使用Session

解析Asp.net如何使用Session

Y2J
Y2J原創
2017-05-10 10:49:211522瀏覽

Session 是保存使用者和Web 應用程式的會話狀態的一種方法,ASP.NET Core 提供了一個用於管理會話狀態的中間件,本篇文章主要介紹了Asp.net Core中使用Session ,有興趣的可以了解一下、

前言

2017年就這麼悄無聲息的開始了,2017年對我來說又是特別重要的一年。

元旦放假在家寫了個Asp.net Core驗證碼登錄, 做demo的過程中遇到兩個小問題,第一是在Asp.net Core引用dll,以往我們引用DLL都是直接引用,在Core裡這樣是不行的,必須基於NuGet添加,或者基於project.json添加,然後保存VS會啟動還原類別庫

第二就是使用Session的問題,Core裡使用Session需要加入Session類別庫。

新增Session

在你的專案上是基於NuGet新增:Microsoft.AspNetCore.Session

修改startup.cs

在startup.cs找到方法ConfigureServices(IServiceCollection services) 注入Session(這個地方是Asp.net Core pipeline):services.AddSession();

接下來我們要告訴Asp.net Core使用記憶體儲存Session數據,在Configure(IApplicationBuilder app,...)中加入程式碼:app.UserSession( ); 

Session

1、在MVC Controller裡使用HttpContext.Session

using Microsoft.AspNetCore.Http;

public class HomeController:Controller
{
   public IActionResult Index()
   {
       HttpContext.Session.SetString("code","123456");
       return View(); 
    }

    public IActionResult About()
    {
       ViewBag.Code=HttpContext.Session.GetString("code");
       return View();
    }
}

2、如果不是在Controller裡,你可以注入IHttpContextAccessor

public class SomeOtherClass
{
   private readonly IHttpContextAccessor _httpContextAccessor;
   private ISession _session=> _httpContextAccessor.HttpContext.Session;

   public SomeOtherClass(IHttpContextAccessor httpContextAccessor)
   {
      _httpContextAccessor=httpContextAccessor;       
   }

   public void Set()
   {
     _session.SetString("code","123456");
   }
  
   public void Get()
  {
     string code = _session.GetString("code");
   }
}

儲存複雜物件

儲存物件時把物件序列化#為json 字串儲存。

public static class SessionExtensions
{
   public static void SetObjectAsJson(this ISession session, string key, object value)
  {
    session.SetString(key, JsonConvert.SerializeObject(value));
  }

  public static T GetObjectFromJson(this ISession session, string key)
  {
    var value = session.GetString(key);

    return value == null ? default(T) : JsonConvert.DeserializeObject(value);
  }
}
var myComplexObject = new MyClass();
HttpContext.Session.SetObjectAsJson("Test", myComplexObject);


var myComplexObject = HttpContext.Session.GetObjectFromJson("Test");

使用SQL Server或Redis儲存

1、SQL Server

新增引用  "Microsoft.Extensions.Caching. SqlServer": "1.0.0"

注入:

// Microsoft SQL Server implementation of IDistributedCache.
// Note that this would require setting up the session state database.
services.AddSqlServerCache(o =>
{
  o.ConnectionString = "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
  o.SchemaName = "dbo";
  o.TableName = "Sessions";
});

2、Redis

新增引用   "Microsoft.Extensions.Caching.Redis": "1.0.0"

注入:

// Redis implementation of IDistributedCache.
// This will override any previously registered IDistributedCache service.
services.AddSingleton();

【相關推薦】

1. ASP免費影片教學

2. ASP教學

3. 李炎恢ASP基礎影片教學

#

以上是解析Asp.net如何使用Session的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn