zoukankan      html  css  js  c++  java
  • ASP.NET Core 应用程序状态

      在ASP.NET Core中,由多种途径可以对应用程序状态进行管理,使用哪种途径,由检索状态的时机和方式决定。

      应用程序状态指的是用于描述当前状况的任意数据。包括全局和用户特有的数据。

      开发人员可以根据不同的因素来选择不同的方式存储状态数据:

        数据需要存储多久

        数据有多大

        数据的格式是什么

        数据是否可以序列化

        数据有多敏感

        数据能否保存在客户端

     1.可选方式

      1.HttpContext.Items

      当数据仅用于一个请求中时,用Items集合存储时最好的方式。数据将在每个请求结束之后丢弃。它是组件和中间件在一个请求中的不同时间点金总互相通信的最佳手段。

      HttpContext抽象提供了一个简单的IDictionary<object,object>类型的字典集合,就是Items。在每个请求中,这个集合从HttpRequest开始就可以使用,直到请求结束丢弃。要想存取集合,可以直接赋值和根据键查询。

     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发送数据,可以将一个请求的状态数据提供给另一个请求。这不适合敏感数据,因为这需要将数据发送到客户端,然后再发送给服务器。这种方法也只适用于少量数据。用户提交的数据是无法预期的,带查询字符串的网址很容易泄露,所以要避免跨网站请求伪装攻击(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的。

      (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.
            //
            // 参数:
            //   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 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[]数组还原         
            /// </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);
                }
            }

      

  • 相关阅读:
    Spring Boot+Vue前后端分离(1):准备环境 +项目启动
    Git使用SSH协议clone项目及SSH简介
    Intelij ideas windows版本快捷键整理
    Spring Boot配置—— yml 配置map
    Nginx配置——多站点配置
    String分割
    Spring日志处理——logger占位符
    Java缓存经验
    maven项目——maven跳过单元测试maven.test.skip和skipTests的区别
    Java工具类——Hutool Java
  • 原文地址:https://www.cnblogs.com/afei-24/p/10990121.html
Copyright © 2011-2022 走看看