{"id":1010,"date":"2018-02-06T12:23:01","date_gmt":"2018-02-06T11:23:01","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=1010"},"modified":"2020-09-01T12:50:19","modified_gmt":"2020-09-01T11:50:19","slug":"c-formsauthentication","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-formsauthentication\/","title":{"rendered":"C# FormsAuthentication"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections.Generic;\nusing System.Diagnostics;\nusing System.Net;\nusing System.Net.Http;\nusing System.Net.Http.Headers;\nusing System.Web.Http;\nusing System.Web.Security;\n\n    public class CoreApiController : ApiController \/\/ Geen BaseApiController!\n    {\n[HttpGet]\n        [Route(\"api\/formsauthentication\")]\n        public HttpResponseMessage GetFormsAuthentication()\n        {\n            var userName = SandboxProvider.GetCurrentUserName();\n            if (string.IsNullOrEmpty(userName))\n            {\n                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, \"Not authenticated\");\n            }\n\n\n            \/\/var cookies = Request.Headers.GetCookies(FormsAuthentication.FormsCookieName);\n            \/\/if (cookies.Count > 0)\n            \/\/{\n            \/\/    return Request.CreateResponse(\"OK\");\n            \/\/}\n\n            var authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(120), false, userName);\n            var response = Request.CreateResponse();\n            var cookie = FormsAuthentication.GetAuthCookie(userName, false);\n            var cookieHeader = new CookieHeaderValue(cookie.Name, cookie.Value);\n            cookieHeader.Domain = cookie.Domain;\n            cookieHeader.Path = cookie.Path;\n            cookieHeader.HttpOnly = true;\n            \/\/cookieHeader.Expires = cookie.Expires;\n\n            response.Headers.AddCookies(new List&lt;CookieHeaderValue> { cookieHeader });\n            response.Headers.CacheControl = new CacheControlHeaderValue\n            {\n                NoStore = true,\n                NoCache = true,\n                MustRevalidate = true\n            };\n            return response;\n        }\n}\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":3995,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1010\/revisions\/3995"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}