You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
16 KiB
JavaScript

"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[4638],{5453:(e,n,s)=>{s.r(n),s.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>h,frontMatter:()=>i,metadata:()=>c,toc:()=>a});var t=s(5893),r=s(1151);s(4996);const i={id:"sesssion-state",title:"32. \u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",sidebar_label:"32. \u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406"},o=void 0,c={id:"sesssion-state",title:"32. \u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",description:"32.1 \u5173\u4e8e\u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",source:"@site/docs/sesssion-state.mdx",sourceDirName:".",slug:"/sesssion-state",permalink:"/docs/sesssion-state",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/sesssion-state.mdx",tags:[],version:"current",lastUpdatedBy:"MonkSoul",lastUpdatedAt:1651894493,formattedLastUpdatedAt:"May 7, 2022",frontMatter:{id:"sesssion-state",title:"32. \u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",sidebar_label:"32. \u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406"},sidebar:"docs",previous:{title:"31. \u865a\u62df\u6587\u4ef6\u7cfb\u7edf\uff08\u4e0a\u4f20\u4e0b\u8f7d\uff09",permalink:"/docs/file-provider"},next:{title:"33. IPC \u8fdb\u7a0b\u901a\u4fe1",permalink:"/docs/ipc"}},l={},a=[{value:"32.1 \u5173\u4e8e\u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",id:"321-\u5173\u4e8e\u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",level:2},{value:"32.2 \u5982\u4f55\u4f7f\u7528",id:"322-\u5982\u4f55\u4f7f\u7528",level:2},{value:"32.2.1 <code>Cookie</code> \u4f7f\u7528",id:"3221-cookie-\u4f7f\u7528",level:3},{value:"32.2.2 <code>Session</code> \u4f7f\u7528",id:"3222-session-\u4f7f\u7528",level:3},{value:"32.2.3 <code>Query Strings</code> \u4f7f\u7528",id:"3223-query-strings-\u4f7f\u7528",level:3},{value:"32.2.4 <code>HttpContext.Items</code> \u4f7f\u7528",id:"3224-httpcontextitems-\u4f7f\u7528",level:3},{value:"32.2.5 <code>Cache</code> \u65b9\u5f0f",id:"3225-cache-\u65b9\u5f0f",level:3},{value:"32.2.6 <code>AsyncLocal&lt;T&gt;</code> \u65b9\u5f0f",id:"3226-asynclocalt-\u65b9\u5f0f",level:3},{value:"32.3 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"323-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function d(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.h2,{id:"321-\u5173\u4e8e\u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406",children:"32.1 \u5173\u4e8e\u4f1a\u8bdd\u548c\u72b6\u6001\u7ba1\u7406"}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"HTTP"})," \u662f\u65e0\u72b6\u6001\u7684\u534f\u8bae\u3002 \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c",(0,t.jsx)(n.code,{children:"HTTP"})," \u8bf7\u6c42\u662f\u4e0d\u4fdd\u7559\u7528\u6237\u503c\u7684\u72ec\u7acb\u6d88\u606f\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u4fdd\u7559\u8bf7\u6c42\u7528\u6237\u6570\u636e\uff1a"]}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.code,{children:"Cookie"}),"\uff1a\u901a\u5e38\u5b58\u50a8\u5728\u5ba2\u6237\u7aef\u7684\u6570\u636e\uff0c\u8bf7\u6c42\u65f6\u5e26\u56de\u670d\u52a1\u7aef"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.code,{children:"Session"}),"\uff1a\u5b58\u50a8\u5728\u670d\u52a1\u7aef\u7684\u6570\u636e\uff08\u53ef\u4ee5\u5728\u5b58\u50a8\u5728\u5185\u5b58\u3001\u8fdb\u7a0b\u7b49\u4ecb\u8d28\u4e2d\uff09"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.code,{children:"Query Strings"}),"\uff1a\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"Http"})," \u8bf7\u6c42\u5730\u5740\u53c2\u6570\u5171\u4eab"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.code,{children:"HttpContext.Items"}),"\uff1a\u5b58\u50a8\u5728\u670d\u52a1\u7aef\uff0c\u53ea\u5728\u8bf7\u6c42\u58f0\u660e\u5468\u671f\u5185\u4f7f\u7528\uff0c\u8bf7\u6c42\u7ed3\u675f\u81ea\u52a8\u9500\u6bc1"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.code,{children:"Cache"}),"\uff1a\u670d\u52a1\u7aef\u7f13\u5b58\uff0c\u5305\u62ec\u5185\u5b58\u7f13\u5b58\u3001\u5206\u5e03\u5f0f\u5185\u5b58\u7f13\u5b58\u3001IO \u7f13\u5b58\u3001\u5e8f\u5217\u5316\u7f13\u5b58\u4ee5\u53ca\u6570\u636e\u5e93\u7f13\u5b58"]}),"\n",(0,t.jsxs)(n.li,{children:[(0,t.jsx)(n.code,{children:"AsyncLocal<T>"}),"\uff1a\u901a\u8fc7\u5f02\u6b65\u63a7\u5236\u6d41\u5b9e\u73b0\u672c\u5730\u6570\u636e\u5171\u4eab\uff0c\u8de8\u7ebf\u7a0b"]}),"\n"]}),"\n",(0,t.jsx)(n.h2,{id:"322-\u5982\u4f55\u4f7f\u7528",children:"32.2 \u5982\u4f55\u4f7f\u7528"}),"\n",(0,t.jsxs)(n.h3,{id:"3221-cookie-\u4f7f\u7528",children:["32.2.1 ",(0,t.jsx)(n.code,{children:"Cookie"})," \u4f7f\u7528"]}),"\n",(0,t.jsxs)(n.p,{children:["\u4f7f\u7528 ",(0,t.jsx)(n.code,{children:"Cookie"})," \u975e\u5e38\u7b80\u5355\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:'// \u8bfb\u53d6 Cookies\r\nvar value = httpContext.Request.Cookies["key"];\r\n\r\n// \u8bbe\u7f6e Cookies\r\nvar option = new CookieOptions();\r\noption.Expires = DateTime.Now.AddMilliseconds(10);\r\nhttpContext.Response.Cookies.Append(key, value, option);\r\n\r\n// \u5220\u9664 Cookies\r\nhttpContext.Response.Cookies.Delete(key);\n'})}),"\n",(0,t.jsx)(n.admonition,{title:"\u7279\u522b\u8bf4\u660e",type:"note",children:(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"httpContext"})," \u53ef\u4ee5\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"IHttpContextAccessor"})," \u83b7\u53d6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"App.HttpContext"})," \u83b7\u53d6\u3002"]})}),"\n",(0,t.jsxs)(n.p,{children:["\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"Cookie"})," \u5b9e\u73b0\u6388\u6743\u529f\u80fd\u53ca\u5355\u70b9\u767b\u5f55\uff08SSO\uff09\uff1a",(0,t.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/aspnet/core/security/cookie-sharing?view=aspnetcore-5.0",children:"\u7f51\u7ad9\u5171\u4eab Cookie"})]}),"\n",(0,t.jsxs)(n.h3,{id:"3222-session-\u4f7f\u7528",children:["32.2.2 ",(0,t.jsx)(n.code,{children:"Session"})," \u4f7f\u7528"]}),"\n",(0,t.jsxs)(n.p,{children:["\u5728\u4f7f\u7528 ",(0,t.jsx)(n.code,{children:"Session"})," \u4e4b\u524d\uff0c\u5fc5\u987b\u6ce8\u518c ",(0,t.jsx)(n.code,{children:"Session"})," \u670d\u52a1\uff1a\uff08\u5982\u679c"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,5,7-12,32,37,39}",children:'public class Startup\r\n{\r\n public void ConfigureServices(IServiceCollection services)\r\n {\r\n // services.AddDistributedMemoryCache(); \u6846\u67b6\u5185\u90e8\u5df2\u7ecf\u9ed8\u8ba4\u6ce8\u518c\r\n\r\n services.AddSession(options =>\r\n {\r\n options.IdleTimeout = TimeSpan.FromSeconds(10);\r\n options.Cookie.HttpOnly = true;\r\n options.Cookie.IsEssential = true;\r\n }); // \u6ce8\u610f\u5728\u63a7\u5236\u5668\u4e4b\u524d\u6ce8\u518c\uff01\uff01\uff01\uff01\r\n\r\n services.AddControllersWithViews();\r\n }\r\n\r\n public void Configure(IApplicationBuilder app, IWebHostEnvironment env)\r\n {\r\n if (env.IsDevelopment())\r\n {\r\n app.UseDeveloperExceptionPage();\r\n }\r\n else\r\n {\r\n app.UseExceptionHandler("/Home/Error");\r\n app.UseHsts();\r\n }\r\n\r\n app.UseHttpsRedirection();\r\n app.UseStaticFiles();\r\n\r\n app.UseRouting();\r\n\r\n app.UseAuthentication();\r\n app.UseAuthorization();\r\n\r\n app.UseSession();\r\n\r\n app.UseEndpoints(endpoints =>\r\n {\r\n endpoints.MapDefaultControllerRoute();\r\n endpoints.MapRazorPages();\r\n });\r\n }\r\n}\n'})}),"\n",(0,t.jsx)(n.admonition,{title:"\u4e2d\u95f4\u4ef6\u6ce8\u518c\u987a\u5e8f",type:"important",children:(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"app.UseSession()"})," \u5fc5\u987b\u5728 ",(0,t.jsx)(n.code,{children:"app.UseRouting()"})," \u548c ",(0,t.jsx)(n.code,{children:"app.UseEndpoints()"})," ",(0,t.jsx)(n.strong,{children:"\u4e4b\u95f4"}),"\u6ce8\u518c\uff01"]})}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsx)(n.li,{children:"\u5e38\u89c1\u4f8b\u5b50\uff1a"}),"\n"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:'// \u8bfb\u53d6 Session\r\nvar byteArr = httpContext.Session.Get("key"); // \u8fd4\u56de byte[]\r\nvar str = httpContext.Session.GetString("key"); // \u8fd4\u56de string[]\r\nvar num = httpContext.Session.GetInt32("key"); // \u8fd4\u56de int\r\n\r\n// \u8bbe\u7f6e Session\r\nhttpContext.Session.SetString("key", "value"); // \u8bbe\u7f6e\u5b57\u7b26\u4e32\r\nhttpContext.Session.SetInt32("key", 1); // \u8bbe\u7f6e int \u7c7b\u578b\n'})}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsx)(n.li,{children:(0,t.jsx)(n.strong,{children:"\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u4efb\u610f\u7c7b\u578b\u62d3\u5c55\uff1a"})}),"\n"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:"public static class SessionExtensions\r\n{\r\n public static void Set<T>(this ISession session, string key, T value)\r\n {\r\n session.SetString(key, JsonSerializer.Serialize(value));\r\n }\r\n\r\n public static T Get<T>(this ISession session, string key)\r\n {\r\n var value = session.GetString(key);\r\n return value == null ? default : JsonSerializer.Deserialize<T>(value);\r\n }\r\n}\n"})}),"\n",(0,t.jsxs)(n.ul,{children:["\n",(0,t.jsxs)(n.li,{children:["\u9632\u6b62 ",(0,t.jsx)(n.code,{children:"Session ID"})," \u6539\u53d8\u6216 ",(0,t.jsx)(n.code,{children:"Session"})," \u5931\u6548"]}),"\n"]}),"\n",(0,t.jsxs)(n.p,{children:["\u5728 ",(0,t.jsx)(n.code,{children:"Startup.cs"})," \u7684 ",(0,t.jsx)(n.code,{children:"ConfigureServices"})," \u914d\u7f6e\u5373\u53ef\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:"services.Configure<CookiePolicyOptions>(options =>\r\n{\r\n \u3000\u3000options.CheckConsentNeeded = context => false; // \u9ed8\u8ba4\u4e3atrue\uff0c\u6539\u4e3afalse\r\n\u3000\u3000 options.MinimumSameSitePolicy = SameSiteMode.None;\r\n});\n"})}),"\n",(0,t.jsxs)(n.h3,{id:"3223-query-strings-\u4f7f\u7528",children:["32.2.3 ",(0,t.jsx)(n.code,{children:"Query Strings"})," \u4f7f\u7528"]}),"\n",(0,t.jsxs)(n.p,{children:["\u8be5\u65b9\u5f0f\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700 ",(0,t.jsx)(n.code,{children:'httpContext.Request.Query["key"]'})," \u5373\u53ef\u3002"]}),"\n",(0,t.jsxs)(n.h3,{id:"3224-httpcontextitems-\u4f7f\u7528",children:["32.2.4 ",(0,t.jsx)(n.code,{children:"HttpContext.Items"})," \u4f7f\u7528"]}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"HttpContext"})," \u5bf9\u8c61\u63d0\u4f9b\u4e86 ",(0,t.jsx)(n.code,{children:"Items"})," \u96c6\u5408\u5c5e\u6027\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u5728\u5355\u6b21\u8bf7\u6c42\u95f4\u5171\u4eab\u6570\u636e\uff0c\u8bf7\u6c42\u7ed3\u675f\u7acb\u5373\u9500\u6bc1\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u6570\u636e\u3002\u4f7f\u7528\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:'// \u8bfb\u53d6\r\nvar value = httpContext.Items["key"];\r\n\r\n// \u6dfb\u52a0\r\nhttpContext.Items["key"] = "\u4efb\u4f55\u503c\u5305\u62ec\u5bf9\u8c61";\r\n\r\n// \u5220\u9664\r\nhttpContext.Items.Remove("key");\n'})}),"\n",(0,t.jsxs)(n.h3,{id:"3225-cache-\u65b9\u5f0f",children:["32.2.5 ",(0,t.jsx)(n.code,{children:"Cache"})," \u65b9\u5f0f"]}),"\n",(0,t.jsxs)(n.p,{children:["\u53c2\u89c1 ",(0,t.jsx)(n.a,{href:"/docs/cache",children:"\u5206\u5e03\u5f0f\u7f13\u5b58"})," \u6587\u6863"]}),"\n",(0,t.jsxs)(n.h3,{id:"3226-asynclocalt-\u65b9\u5f0f",children:["32.2.6 ",(0,t.jsx)(n.code,{children:"AsyncLocal<T>"})," \u65b9\u5f0f"]}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"AsyncLocal<T>"})," \u53ef\u4ee5\u8bf4\u662f\u8fdb\u7a0b\u5185\u5171\u4eab\u6570\u636e\u7684\u5927\u5229\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u8be5\u7c7b\u5b9e\u73b0\u8de8\u7ebf\u7a0b\u3001\u5f02\u6b65\u63a7\u5236\u6d41\u4e2d\u5171\u4eab\u6570\u636e\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:"using System;\r\nusing System.Threading;\r\nusing System.Threading.Tasks;\r\n\r\nclass Example\r\n{\r\n static AsyncLocal<string> _asyncLocalString = new AsyncLocal<string>();\r\n\r\n static ThreadLocal<string> _threadLocalString = new ThreadLocal<string>();\r\n\r\n static async Task AsyncMethodA()\r\n {\r\n // Start multiple async method calls, with different AsyncLocal values.\r\n // We also set ThreadLocal values, to demonstrate how the two mechanisms differ.\r\n _asyncLocalString.Value = \"Value 1\";\r\n _threadLocalString.Value = \"Value 1\";\r\n var t1 = AsyncMethodB(\"Value 1\");\r\n\r\n _asyncLocalString.Value = \"Value 2\";\r\n _threadLocalString.Value = \"Value 2\";\r\n var t2 = AsyncMethodB(\"Value 2\");\r\n\r\n // Await both calls\r\n await t1;\r\n await t2;\r\n }\r\n\r\n static async Task AsyncMethodB(string expectedValue)\r\n {\r\n Console.WriteLine(\"Entering AsyncMethodB.\");\r\n Console.WriteLine(\" Expected '{0}', AsyncLocal value is '{1}', ThreadLocal value is '{2}'\",\r\n expectedValue, _asyncLocalString.Value, _threadLocalString.Value);\r\n await Task.Delay(100);\r\n Console.WriteLine(\"Exiting AsyncMethodB.\");\r\n Console.WriteLine(\" Expected '{0}', got '{1}', ThreadLocal value is '{2}'\",\r\n expectedValue, _asyncLocalString.Value, _threadLocalString.Value);\r\n }\r\n\r\n static async Task Main(string[] args)\r\n {\r\n await AsyncMethodA();\r\n }\r\n}\r\n// The example displays the following output:\r\n// Entering AsyncMethodB.\r\n// Expected 'Value 1', AsyncLocal value is 'Value 1', ThreadLocal value is 'Value 1'\r\n// Entering AsyncMethodB.\r\n// Expected 'Value 2', AsyncLocal value is 'Value 2', ThreadLocal value is 'Value 2'\r\n// Exiting AsyncMethodB.\r\n// Expected 'Value 2', got 'Value 2', ThreadLocal value is ''\r\n// Exiting AsyncMethodB.\r\n// Expected 'Value 1', got 'Value 1', ThreadLocal value is ''\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u4e3a\u4e86\u7b80\u5316\u64cd\u4f5c\uff0c",(0,t.jsx)(n.code,{children:"Furion v2.18+"})," \u7248\u672c\u5b9e\u73b0\u4e86\u8f7b\u91cf\u7ea7\u7684 ",(0,t.jsx)(n.code,{children:"CallContext"})," \u9759\u6001\u7c7b\uff0c\u5185\u90e8\u4f7f\u7528 ",(0,t.jsx)(n.code,{children:"AsyncLocal<T>"})," \u5b9e\u73b0\uff0c\u4f7f\u7528\u5982\u4e0b\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:'CallContext.SetLocalValue("name", "Furion");\r\nCallContext.GetLocalValue("name");\r\n\r\nCallContext<int>.SetLocalValue("count", 1);\r\nCallContext<int>.GetLocalValue("count");\n'})}),"\n",(0,t.jsxs)(n.p,{children:["\u4e86\u89e3\u66f4\u591a ",(0,t.jsx)(n.code,{children:"AsyncLocal<T>"})," \u77e5\u8bc6\uff1a",(0,t.jsx)(n.a,{href:"https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.asynclocal-1?redirectedfrom=MSDN&view=net-5.0",children:"https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.asynclocal-1?redirectedfrom=MSDN&view=net-5.0"})]}),"\n",(0,t.jsx)(n.h2,{id:"323-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"32.3 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,t.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,t.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,t.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})})]})}function h(e={}){const{wrapper:n}={...(0,r.a)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},1151:(e,n,s)=>{s.d(n,{Z:()=>c,a:()=>o});var t=s(7294);const r={},i=t.createContext(r);function o(e){const n=t.useContext(i);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:o(e.components),t.createElement(i.Provider,{value:n},e.children)}}}]);