{"id":577,"date":"2019-09-24T14:51:23","date_gmt":"2019-09-24T05:51:23","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=577"},"modified":"2019-09-24T14:51:23","modified_gmt":"2019-09-24T05:51:23","slug":"hello-world-with-cookies-2","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=577","title":{"rendered":"hello-world-with-cookies 2"},"content":{"rendered":"\n<pre class=\"lang:c# decode:true \" >using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder\r\nusing Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder\r\nusing Microsoft.AspNetCore.Http; \/\/WriteAsync\r\nusing Microsoft.AspNetCore; \/\/webHost\r\n\r\nnamespace WebApplication1\r\n{\r\n    public class Startup\r\n    {\r\n        public void Configure(IApplicationBuilder app)\r\n        {\r\n            app.Run(async context =&gt;\r\n            {\r\n                context.Response.Headers.Add(\"content-tyep\", \"text\/html\");\r\n                var deleteCookie = context.Request.Query[\"delete\"];\r\n\r\n                if(!string.IsNullOrWhiteSpace(deleteCookie))\r\n                {\r\n                    context.Response.Cookies.Delete(\"MyCookie\");\r\n                    await context.Response.WriteAsync($@\"Delete cookie. Click &lt;a href=\"\"\\\"\"&gt;here&lt;\/a&gt; to go back to home page.\");\r\n                    return;\r\n                }\r\n\r\n                var cookie = context.Request.Cookies[\"MyCookie\"];\r\n\r\n                if (string.IsNullOrWhiteSpace(cookie) &amp;&amp; context.Request.Path == \"\/\")\r\n                {\r\n                    context.Response.Cookies.Append\r\n                    (\r\n                        \"MyCookie\",\r\n                        \"Hello World\",\r\n                        new CookieOptions\r\n                        {\r\n                            Path = \"\/\",\r\n                            HttpOnly = false,\r\n                            Secure = false\r\n                        }\r\n                    );\r\n                    await context.Response.WriteAsync($\"Writing a new cookie &lt;br\/&gt;Refresh page to see cookie value.&lt;br\/&gt;\");\r\n                }\r\n                else\r\n                {\r\n                    await context.Response.WriteAsync($@\"Click &lt;a href=\"\"\\?delete=1\"\"&gt;here&lt;\/a&gt; to delete cookie.&lt;br\/&gt;\");\r\n                }\r\n            });\r\n        }\r\n    }\r\n\r\n    public class Program\r\n    {\r\n        public static void Main(string[] args)\r\n        {\r\n            CreateWebHostBuilder(args).Build().Run();\r\n        }\r\n\r\n        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;\r\n            WebHost.CreateDefaultBuilder(args)\r\n                .UseStartup&lt;Startup&gt;()\r\n                .UseEnvironment(\"Development\");\r\n    }\r\n}<\/pre>\n<p>\uacb0\uacfc ? :Writing a new cookie<br \/>\nRefresh page to see cookie value.<\/p>\n<p>Click here to delete cookie.<\/p>\n<p>Delete cookie. Click here to go back to home page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using Microsoft.AspNetCore.Hosting; \/\/IWebHostBuilder using Microsoft.AspNetCore.Builder; \/\/IApplicationBuilder using Microsoft.AspNetCore.Http; \/\/WriteAsync using Microsoft.AspNetCore; \/\/webHost namespace WebApplication1 { public class Startup { public void Configure(IApplicationBuilder app) { app.Run(async context =&gt; { context.Response.Headers.Add(&#8220;content-tyep&#8221;, &#8220;text\/html&#8221;); var deleteCookie = context.Request.Query[&#8220;delete&#8221;]; if(!string.IsNullOrWhiteSpace(deleteCookie)) { context.Response.Cookies.Delete(&#8220;MyCookie&#8221;); await context.Response.WriteAsync($@&#8221;Delete cookie. Click &lt;a href=&#8221;&#8221;\\&#8221;&#8221;&gt;here&lt;\/a&gt; to go back to home page.&#8221;); return; } var cookie = context.Request.Cookies[&#8220;MyCookie&#8221;];\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=577\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[108],"tags":[],"class_list":["post-577","post","type-post","status-publish","format-standard","hentry","category-net-core"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=577"}],"version-history":[{"count":1,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":578,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions\/578"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}