ASP.NET?Core管理應(yīng)用程序狀態(tài)
在ASP.NET Core中,由多種途徑可以對應(yīng)用程序狀態(tài)進行管理,使用哪種途徑,由檢索狀態(tài)的時機和方式?jīng)Q定。
應(yīng)用程序狀態(tài)指的是用于描述當(dāng)前狀況的任意數(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
當(dāng)數(shù)據(jù)僅用于一個請求中時,用Items集合存儲時最好的方式。數(shù)據(jù)將在每個請求結(jié)束之后丟棄。它是組件和中間件在一個請求中的不同時間點金總互相通信的最佳手段。
HttpContext抽象提供了一個簡單的IDictionary<object,object>類型的字典集合,就是Items。在每個請求中,這個集合從HttpRequest開始就可以使用,直到請求結(jié)束丟棄。要想存取集合,可以直接賦值和根據(jù)鍵查詢。
app.Use(async (context,next) => { context.Items["isExist"] = true; await next.Invoke(); }); //在之后的管道查詢值 app.Run(async (context) => { await context.Response.WriteAsync("Is Exist:"+context.Items["isExist"]); });
2.QueryString 和 Post
在查詢字符串(QueryString )中添加值,或利用Post發(fā)送數(shù)據(jù),可以將一個請求的狀態(tài)數(shù)據(jù)提供給另一個請求。這不適合敏感數(shù)據(jù),因為這需要將數(shù)據(jù)發(fā)送到客戶端,然后再發(fā)送給服務(wù)器。這種方法也只適用于少量數(shù)據(jù)。用戶提交的數(shù)據(jù)是無法預(yù)期的,帶查詢字符串的網(wǎng)址很容易泄露,所以要避免跨網(wǎng)站請求偽裝攻擊(CSRF)。
3.Cookies
與狀態(tài)有關(guān)的小量數(shù)據(jù)可以存儲在Cookies中。他們會隨每次請求被發(fā)送到客戶端。應(yīng)該只使用一個標識符,真正的數(shù)據(jù)存儲在服務(wù)端,服務(wù)端的數(shù)據(jù)與這個標識關(guān)聯(lián)。
4.Session
會話存儲依靠一個基于Cookie的標識符來訪問與給定瀏覽器相關(guān)的會話數(shù)據(jù)。一個會話可以與多個Cookie關(guān)聯(lián)。
5.Cache
緩存提供了一種方法,用自定義的鍵對應(yīng)用程序數(shù)據(jù)進行存儲和檢索。它提供了一套基于時間和其他因素使緩存過期的規(guī)則。
6.其他
還可以使用EF和數(shù)據(jù)庫等進行存儲應(yīng)用程序狀態(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 在服務(wù)端決定過期時間,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); } }
到此這篇關(guān)于ASP.NET Core管理應(yīng)用程序狀態(tài)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Linux上使用Docker部署ASP.NET?Core應(yīng)用程序
- 使用Supervisor守護ASP.NET?Core應(yīng)用程序進程
- ASP.NET Core應(yīng)用程序配置文件AppSetting.json
- 創(chuàng)建ASP.NET?Core?Web應(yīng)用程序并介紹項目模板
- 在ASP.Net?Core應(yīng)用程序中使用Bootstrap4
- ASP.NET Core 應(yīng)用程序中的靜態(tài)文件中間件的實現(xiàn)
- Asp.Net Core Web應(yīng)用程序—探索
- 如何在ASP.NET Core應(yīng)用程序運行Vue并且部署在IIS上詳解
- 詳解將ASP.NET Core應(yīng)用程序部署至生產(chǎn)環(huán)境中(CentOS7)
相關(guān)文章
TrieTree服務(wù)-組件構(gòu)成及其作用介紹
本文將一步步教你配置和使用TrieTree服務(wù),需要的朋友可以參考下2013-01-01ASP.NET Core AutoWrapper 自定義響應(yīng)輸出實現(xiàn)
這篇文章主要介紹了ASP.NET Core AutoWrapper 自定義響應(yīng)輸出實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08HTTP協(xié)議下用Web Service上傳大文件的解決方案
HTTP協(xié)議下用Web Service上傳大文件的解決方案...2007-04-04ASP.NET(VB)寫的后臺發(fā)送短信實現(xiàn)代碼
使用vb寫的后臺發(fā)送短信代碼,很實用的一項功能,感興趣的朋友可以了解下,或許對你學(xué)習(xí)asp.net vb有所幫助2013-02-02