ASP.NET Core 应用程序状态
在ASP.NET Core中,由多種途徑可以對應用程序狀態(tài)進行管理,使用哪種途徑,由檢索狀態(tài)的時機和方式?jīng)Q定。
應用程序狀態(tài)指的是用于描述當前狀況的任意數(shù)據(jù)。包括全局和用戶特有的數(shù)據(jù)。
開發(fā)人員可以根據(jù)不同的因素來選擇不同的方式存儲狀態(tài)數(shù)據(jù):
數(shù)據(jù)需要存儲多久
數(shù)據(jù)有多大
數(shù)據(jù)的格式是什么
數(shù)據(jù)是否可以序列化
數(shù)據(jù)有多敏感
數(shù)據(jù)能否保存在客戶端
?1.可選方式
1.HttpContext.Items
當數(shù)據(jù)僅用于一個請求中時,用Items集合存儲時最好的方式。數(shù)據(jù)將在每個請求結(jié)束之后丟棄。它是組件和中間件在一個請求中的不同時間點金總互相通信的最佳手段。
HttpContext抽象提供了一個簡單的IDictionary<object,object>類型的字典集合,就是Items。在每個請求中,這個集合從HttpRequest開始就可以使用,直到請求結(jié)束丟棄。要想存取集合,可以直接賦值和根據(jù)鍵查詢。
2.QueryString 和 Post
在查詢字符串(QueryString?)中添加值,或利用Post發(fā)送數(shù)據(jù),可以將一個請求的狀態(tài)數(shù)據(jù)提供給另一個請求。這不適合敏感數(shù)據(jù),因為這需要將數(shù)據(jù)發(fā)送到客戶端,然后再發(fā)送給服務器。這種方法也只適用于少量數(shù)據(jù)。用戶提交的數(shù)據(jù)是無法預期的,帶查詢字符串的網(wǎng)址很容易泄露,所以要避免跨網(wǎng)站請求偽裝攻擊(CSRF)。
3.Cookies
與狀態(tài)有關的小量數(shù)據(jù)可以存儲在Cookies中。他們會隨每次請求被發(fā)送到客戶端。應該只使用一個標識符,真正的數(shù)據(jù)存儲在服務端,服務端的數(shù)據(jù)與這個標識關聯(lián)。
4.Session
會話存儲依靠一個基于Cookie的標識符來訪問與給定瀏覽器相關的會話數(shù)據(jù)。一個會話可以與多個Cookie關聯(lián)。
5.Cache
緩存提供了一種方法,用自定義的鍵對應用程序數(shù)據(jù)進行存儲和檢索。它提供了一套基于時間和其他因素使緩存過期的規(guī)則。
6.其他
還可以使用EF和數(shù)據(jù)庫等進行存儲應用程序狀態(tài)。
2.使用Session
首先要安裝Microsoft.AspNetCore.Session安裝包。然后在Startup類中配置。Session是基于IDistributedCache構(gòu)建的,因此必須先配置好Session,否則會報錯。
services.AddDistributedMemoryCache();services.AddSession(options =>
{
options.Cookie.Name = "Test.Session";
options.IdleTimeout = TimeSpan.FromSeconds(10);
});
ASP.NET 提供了IDistributedCache的多種實現(xiàn),in-memory是其中之一。上面采用in-memory,需要先安裝Microsoft.Extensions.Caching.Memory,然后添加上面代碼。
最后在Configure中調(diào)用?app.UseSession(),需要在app.UseMvc使用之前調(diào)用。
(1)實現(xiàn)細節(jié)
Session利用一個cookie來跟蹤和區(qū)分不同瀏覽器發(fā)出的請求。默認情況下,這個cookie被命名為“.ASP.Session”,并使用路徑“/”。默認情況下,這個cookie不指定域,而且對于頁面的客戶端腳本是不可使用的,因為CookieHttpOnly默認為True。
其他的值可以通過SessionOptions配置:
services.AddSession(options =>{
options.Cookie.Name = "Test.Session";
options.IdleTimeout = TimeSpan.FromSeconds(10);
});
IdleTimeout 在服務端決定過期時間,session的過期時間是獨立于cookie的。
(2)ISession
安裝和配置好session之后,就可以通過HttpContext的一個名為Session,類型為ISession的屬性來引用會話。
public interface ISession{
//
// 摘要:
// Indicate whether the current session has loaded.
bool IsAvailable { get; }
//
// 摘要:
// A unique identifier for the current session. This is not the same as the session
// cookie since the cookie lifetime may not be the same as the session entry lifetime
// in the data store.
string Id { get; }
//
// 摘要:
// Enumerates all the keys, if any.
IEnumerable<string> Keys { get; }
//
// 摘要:
// Remove all entries from the current session, if any. The session cookie is not
// removed.
void Clear();
//
// 摘要:
// Store the session in the data store. This may throw if the data store is unavailable.
Task CommitAsync(CancellationToken cancellationToken = default(CancellationToken));
//
// 摘要:
// Load the session from the data store. This may throw if the data store is unavailable.
Task LoadAsync(CancellationToken cancellationToken = default(CancellationToken));
//
// 摘要:
// Remove the given key from the session if present.
//
// 參數(shù):
// key:
void Remove(string key);
//
// 摘要:
// Set the given key and value in the current session. This will throw if the session
// was not established prior to sending the response.
//
// 參數(shù):
// key:
//
// value:
void Set(string key, byte[] value);
//
// 摘要:
// Retrieve the value of the given key, if present.
//
// 參數(shù):
// key:
//
// value:
bool TryGetValue(string key, out byte[] value);
}
因為Session是建立在IDistributedCache之上的,所以總是需要序列化被存儲的對象實例。因此,這個接口是使用byte[]而不是直接使用object。string 和 int32 的簡單類型可以直接使用:
HttpContext.Session.SetInt32("key",123);HttpContext.Session.GetInt32("key");
存儲對象需要先把對象序列化為一個byte[]字節(jié)流。需要使用MemoryStream?和?BinaryFormatter
/// <summary>/// 將一個object對象序列化,返回一個byte[]
/// </summary>
/// <param name="obj">能序列化的對象</param>
/// <returns></returns>
public static byte[] ObjectToBytes(object obj)
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer();
}
}
/// <summary>
/// 將一個序列化后的byte[]數(shù)組還原
/// </summary>
/// <param name="Bytes"></param>
/// <returns></returns>
public static object BytesToObject(byte[] Bytes)
{
using (MemoryStream ms = new MemoryStream(Bytes))
{
IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms);
}
}
原文地址:https://www.cnblogs.com/afei-24/p/10990121.html
.NET社區(qū)新聞,深度好文,歡迎訪問公眾號文章匯總?http://www.csharpkit.com?
總結(jié)
以上是生活随笔為你收集整理的ASP.NET Core 应用程序状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AKS使用Azure File实现动态持
- 下一篇: asp.net core 集成JWT