网站建设网站建设哪里有,在五八同城做网站多少钱,手机网站建设书籍,厦门seo网站关键词优推广在ASP.NET Core中#xff0c;由多种途径可以对应用程序状态进行管理#xff0c;使用哪种途径#xff0c;由检索状态的时机和方式决定。应用程序状态指的是用于描述当前状况的任意数据。包括全局和用户特有的数据。开发人员可以根据不同的因素来选择不同的方式存储状态数据由多种途径可以对应用程序状态进行管理使用哪种途径由检索状态的时机和方式决定。 应用程序状态指的是用于描述当前状况的任意数据。包括全局和用户特有的数据。 开发人员可以根据不同的因素来选择不同的方式存储状态数据 数据需要存储多久 数据有多大 数据的格式是什么 数据是否可以序列化 数据有多敏感 数据能否保存在客户端 1.可选方式 1.HttpContext.Items 当数据仅用于一个请求中时用Items集合存储时最好的方式。数据将在每个请求结束之后丢弃。它是组件和中间件在一个请求中的不同时间点金总互相通信的最佳手段。 HttpContext抽象提供了一个简单的IDictionaryobject,object类型的字典集合就是Items。在每个请求中这个集合从HttpRequest开始就可以使用直到请求结束丢弃。要想存取集合可以直接赋值和根据键查询。 2.QueryString 和 Post 在查询字符串QueryString 中添加值或利用Post发送数据可以将一个请求的状态数据提供给另一个请求。这不适合敏感数据因为这需要将数据发送到客户端然后再发送给服务器。这种方法也只适用于少量数据。用户提交的数据是无法预期的带查询字符串的网址很容易泄露所以要避免跨网站请求伪装攻击CSRF。 3.Cookies 与状态有关的小量数据可以存储在Cookies中。他们会随每次请求被发送到客户端。应该只使用一个标识符真正的数据存储在服务端服务端的数据与这个标识关联。 4.Session 会话存储依靠一个基于Cookie的标识符来访问与给定浏览器相关的会话数据。一个会话可以与多个Cookie关联。 5.Cache 缓存提供了一种方法用自定义的键对应用程序数据进行存储和检索。它提供了一套基于时间和其他因素使缓存过期的规则。 6.其他 还可以使用EF和数据库等进行存储应用程序状态。2.使用Session 首先要安装Microsoft.AspNetCore.Session安装包。然后在Startup类中配置。Session是基于IDistributedCache构建的因此必须先配置好Session否则会报错。 services.AddDistributedMemoryCache(); services.AddSession(options { options.Cookie.Name Test.Session; options.IdleTimeout TimeSpan.FromSeconds(10); }); ASP.NET 提供了IDistributedCache的多种实现in-memory是其中之一。上面采用in-memory需要先安装Microsoft.Extensions.Caching.Memory然后添加上面代码。 最后在Configure中调用 app.UseSession()需要在app.UseMvc使用之前调用。 1实现细节 Session利用一个cookie来跟踪和区分不同浏览器发出的请求。默认情况下这个cookie被命名为“.ASP.Session”并使用路径“/”。默认情况下这个cookie不指定域而且对于页面的客户端脚本是不可使用的因为CookieHttpOnly默认为True。 其他的值可以通过SessionOptions配置 services.AddSession(options { options.Cookie.Name Test.Session; options.IdleTimeout TimeSpan.FromSeconds(10); }); IdleTimeout 在服务端决定过期时间session的过期时间是独立于cookie的。 2ISession 安装和配置好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. IEnumerablestring 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.//// 参数:// 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.//// 参数:// key://// value:void Set(string key, byte[] value);//// 摘要:// Retrieve the value of the given key, if present.//// 参数:// 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[]字节流。需要使用MemoryStream 和 BinaryFormatter/// summary /// 将一个object对象序列化返回一个byte[] /// /summary /// param nameobj能序列化的对象/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[]数组还原 /// /summary/// param nameBytes/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社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com