{"id":235,"date":"2020-07-07T08:23:35","date_gmt":"2020-07-06T23:23:35","guid":{"rendered":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=235"},"modified":"2020-07-07T11:47:00","modified_gmt":"2020-07-07T02:47:00","slug":"asp-net-core%e3%81%a7%e3%81%ae%e3%82%bb%e3%83%83%e3%82%b7%e3%83%a7%e3%83%b3%e5%a4%89%e6%95%b0","status":"publish","type":"post","link":"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=235","title":{"rendered":"asp.net core\u3067\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570"},"content":{"rendered":"\n<p>asp.net core\u3067\u306f\u30bb\u30c3\u30b7\u30e7\u30f3\u7b49\u306f\u57fa\u672c\u7684\u306b\u306f\u30b5\u30fc\u30d0\u30fc\u4e0a\u3067\u7ba1\u7406\u3057\u306a\u3044\u3002<\/p>\n\n\n\n<p>asp.net\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u3092asp.net core\u3067\u307b\u307c\u540c\u69d8\u306b\u4f7f\u7528\u3059\u308b\u306b\u306f\u3001Microsoft.AspNetCore.Session\u3092\u4f7f\u7528\u3059\u308b\u3002<\/p>\n\n\n\n<p>Microsoft.AspNetCore.Session\u3067\u306f\u3001Cookie\u3092\u4f7f\u7528\u3057\u3066\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u60c5\u5831\u3092\u4fdd\u6301\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u30bb\u30c3\u30b7\u30e7\u30f3\u3092aps.net core\u3067\u4f7f\u7528\u3059\u308b\u305f\u3081\u306b\u306f\u3001Startup\u3068Configure\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"Startup.cs\" data-lang=\"C#\"><code>public void ConfigureServices(IServiceCollection services)\n{\n\t\/\/ \u5206\u6563\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u6307\u5b9a(\u30a2\u30d7\u30ea\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5185\u3067\u6709\u52b9)\n    services.AddDistributedMemoryCache();\n\t\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u30b5\u30fc\u30d3\u30b9\u306e\u8ffd\u52a0\n    services.AddSession(opt=&gt;{\n\t\t\/\/ \u30aa\u30d7\u30b7\u30e7\u30f3\u6307\u5b9a\n        opt.IdleTimeout = TimeSpan.FromMinutes(20);\n        opt.Cookie.IsEssential = true;\n    });\n\t\u30fb\u30fb\u30fb\n}\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n\t\u30fb\u30fb\u30fb\n    app.UseRouting();\n\n    app.UseAuthorization();\n\n\t\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\n    app.UseSession();\n\t\u30fb\u30fb\u30fb\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3057\u305f\u308a\u3001\u53d6\u5f97\u3059\u308b\u306b\u306f\u3001\u6697\u9ed9\u3067\u5ba3\u8a00\u3055\u308c\u3066\u3044\u308b\u3001HttpContext\u5909\u6570\u3092\u4f7f\u7528\u3059\u308b\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"\u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u3078\u306e\u30a2\u30af\u30bb\u30b9\" data-lang=\"C#\"><code>\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u3078\u5024\u3092\u30bb\u30c3\u30c8(\u6587\u5b57\u5217)\nHttpContext.Session.SetString(&quot;MyString&quot;,&quot;Hello&quot;);\n\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u3078\u5024\u3092\u30bb\u30c3\u30c8(\u6574\u6570)\nHttpContext.Session.SetInt32(&quot;MyInt&quot;,123);\n\n\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u304b\u3089\u5024\u3092\u53d6\u5f97(\u6587\u5b57\u5217)\nstring mystring = HttpContext.Session.GetString(&quot;MyString&quot;);\n\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u304b\u3089\u5024\u3092\u53d6\u5f97(\u6574\u6570)\nint myint = HttpContext.Session.GetInt32(&quot;MyInt&quot;);\n\n\/\/ \u30af\u30e9\u30b9\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u7b49\u3092\u4fdd\u5b58\u3059\u308b\u306b\u306f\u3001Json\u7b49\u3067\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\nclass MyClass {\n\tpublic string MyString { get; set; }\n\tpublic int MyInt { get; set; }\n}\n\u30fb\u30fb\u30fb\nMyClass c = new MyClass() { MyString=&quot;Hello&quot;, MyInt = 123 };\n\/\/ JSON\u6587\u5b57\u5217\u3078\u5909\u63db\nstring sessvalue = JsonSerializer.Serialize(c);\n\/\/ \u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u3078\u6587\u5b57\u5217\u3068\u3057\u3066\u4fdd\u5b58\nHttpContext.Session.SetString(&quot;MyClassInsance&quot;, sessvalue);\n\u30fb\u30fb\u30fb\n\/\/ \u6587\u5b57\u5217\u304b\u3089\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3078\u30c7\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\nMyClass cc = JsonSerializer.Deserialize&lt;MyClass&gt;(HttpContext.Session.GetString(&quot;MyClassInstance&quot;));<\/code><\/pre><\/div>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u3063\u3066\u3082\u826f\u3044\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-csharp\" data-file=\"\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\" data-lang=\"C#\"><code>public static class SessionExtention {\n    public static T Get&lt;T&gt;(this ISession session, string key) {\n        string val = session.GetString(key);\n        return val == null ? default : JsonSerializer.Deserialize&lt;T&gt;(val);\n    }\n    public static void Set&lt;T&gt;(this ISession session, string key, T val) {\n        string jsonstr = JsonSerializer.Serialize(val);\n        session.SetString(key,jsonstr);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u3068\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u30bb\u30c3\u30b7\u30e7\u30f3\u5909\u6570\u3092\u6271\u3046\u3053\u3068\u304c\u53ef\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>asp.net core\u3067\u306f\u30bb\u30c3\u30b7\u30e7\u30f3\u7b49\u306f\u57fa\u672c\u7684\u306b\u306f\u30b5\u30fc\u30d0\u30fc\u4e0a\u3067\u7ba1\u7406\u3057\u306a\u3044\u3002 a &hellip; <a href=\"https:\/\/sumomo.ohwaki.jp\/wordpress\/?p=235\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,9,4],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-c","category-4"],"_links":{"self":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=235"}],"version-history":[{"count":6,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sumomo.ohwaki.jp\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}