{"id":688,"date":"2017-03-23T13:25:49","date_gmt":"2017-03-23T12:25:49","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=688"},"modified":"2017-03-23T13:25:49","modified_gmt":"2017-03-23T12:25:49","slug":"c-basic-authentication","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-basic-authentication\/","title":{"rendered":"C# Basic Authentication"},"content":{"rendered":"<pre lang=\"C#\">\r\nHttpRequestBase request = actionContext.HttpContext.Request; \r\nstring userName = \"\";\r\nstring password = \"\";\r\n\r\nbool authorized = false;\r\n\r\nString basicAuthorizationHeaderEncoded = request.Headers.Get(\"Authorization\");\r\nif (!String.IsNullOrEmpty(basicAuthorizationHeaderEncoded))\r\n{\r\n basicAuthorizationHeaderEncoded = basicAuthorizationHeaderEncoded.Split(' ')[1];\r\n String basicAuthorizationHeaderDecoded = String.Empty.FromBase64(basicAuthorizationHeaderEncoded);\r\n String[] authorizationHeaderValues = basicAuthorizationHeaderDecoded.Split(new char[] { ':' });\r\n if (authorizationHeaderValues.Count() == 2)\r\n {\r\n userName = authorizationHeaderValues.First();\r\n password = authorizationHeaderValues.Last();\r\n }\r\n}\r\nelse\r\n{\r\n var userInfo = request.Url.UserInfo;\r\n if (!String.IsNullOrWhiteSpace(userInfo))\r\n {\r\n var parts = userInfo.Split(':');\r\n userName = parts[0] ?? \"\";\r\n password = parts[1] ?? \"\";\r\n }\r\n}\r\n\r\nif (!String.IsNullOrEmpty(userName) &amp;&amp; !String.IsNullOrEmpty(password))\r\n{\r\n var webpartServiceUserName = Config.DynamicWebservice.GetSection().Settings[\"WebpartServiceUserName\"].Value;\r\n var webpartServicePassword = Config.DynamicWebservice.GetSection().Settings[\"WebpartServicePassword\"].Value;\r\n\r\n if (userName == webpartServiceUserName &amp;&amp; password == webpartServicePassword)\r\n {\r\n authorized = true;\r\n }\r\n}\r\n\r\nif (!authorized)\r\n{\r\n HttpContext.Current.Response.SuppressFormsAuthenticationRedirect = true;\r\n HttpContext.Current.Response.StatusCode = 401;\r\n HttpContext.Current.Response.Headers.Add(\"WWW-Authenticate\", String.Empty);\r\n HttpContext.Current.Response.Buffer = false; \/\/ Disable buffer and flush output to prevent forms authentication redirect\r\n HttpContext.Current.Response.BufferOutput = false;\r\n\r\n HttpContext.Current.Response.Write(\"Not authorized\");\r\n\r\n HttpContext.Current.Response.Flush();\r\n HttpContext.Current.Response.OutputStream.Flush();\r\n HttpContext.Current.Response.End();\r\n throw new Exception(\"Not authorized\");\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>HttpRequestBase request = actionContext.HttpContext.Request; string userName = &#8220;&#8221;; string password = &#8220;&#8221;; bool authorized = false; String basicAuthorizationHeaderEncoded = request.Headers.Get(&#8220;Authorization&#8221;); if (!String.IsNullOrEmpty(basicAuthorizationHeaderEncoded)) { basicAuthorizationHeaderEncoded = basicAuthorizationHeaderEncoded.Split(&#8216; &#8216;)[1]; String basicAuthorizationHeaderDecoded = String.Empty.FromBase64(basicAuthorizationHeaderEncoded); String[] authorizationHeaderValues = basicAuthorizationHeaderDecoded.Split(new char[] { &#8216;:&#8217; }); if (authorizationHeaderValues.Count() == 2) { userName = authorizationHeaderValues.First(); password = authorizationHeaderValues.Last(); } } else { var [&hellip;]<\/p>\n","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-688","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/688","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=688"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/688\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/688\/revisions\/689"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}