{"id":1085,"date":"2014-03-20T13:56:15","date_gmt":"2014-03-20T13:56:15","guid":{"rendered":"http:\/\/www.rezafaisal.net\/?p=1085"},"modified":"2014-03-22T01:51:52","modified_gmt":"2014-03-22T01:51:52","slug":"enforcing-ssl-in-a-web-api-controller","status":"publish","type":"post","link":"http:\/\/www.rezafaisal.net\/?p=1085","title":{"rendered":"Enforcing SSL in a Web API Controller"},"content":{"rendered":"<p>Pada posting sebelumnya telah diterangkan bagaimana menambahkan Web API Controller pada project website ASP.NET Web Form (<a title=\"http:\/\/www.rezafaisal.net\/?p=1049\" href=\"http:\/\/www.rezafaisal.net\/?p=1049\">http:\/\/www.rezafaisal.net\/?p=1049<\/a>). Pada posting yang lain juga diterangkan bagaimana mengaktifkan SSL pada IIS Express via Visual Studio 2013 <a title=\"http:\/\/www.rezafaisal.net\/?p=1064\" href=\"http:\/\/www.rezafaisal.net\/?p=1064\">http:\/\/www.rezafaisal.net\/?p=1064<\/a>.<\/p>\n<p>Untuk mengamankan Web API yang telah dibuat, maka langkah awal yang dapat dilakukan adalah \u201cmenempatkan\u201d Web API Controller agar hanya dapat diakses pada jalur SLL. Pada posting ini akan diterangkan langkah-langkah agar Web API Controller yang telah dibuat hanya dapat diakses lewat jalur HTTPS saja.<\/p>\n<p>Langkah pertama adalah membuat class HarusPakaiHttpsAttribute yang nantinya dapat digunakan sebagai tambahan atribut pada class Web API Controller, jika bekerja pada project Website maka class ini harus berada pada folder App_Code, sedangkan jika digunakan berikut adalah kode dari class HarusPakaiHttpsAttribute :<\/p>\n<div id=\"codeSnippetWrapper\" style=\"overflow: auto; cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<div id=\"codeSnippet\" style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum1\" style=\"color: #606060;\">   1:<\/span> <span style=\"color: #0000ff;\">using<\/span> System;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum2\" style=\"color: #606060;\">   2:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Collections.Generic;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum3\" style=\"color: #606060;\">   3:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Linq;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum4\" style=\"color: #606060;\">   4:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Net.Http;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum5\" style=\"color: #606060;\">   5:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum6\" style=\"color: #606060;\">   6:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web.Http.Controllers;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum7\" style=\"color: #606060;\">   7:<\/span> <span style=\"color: #0000ff;\">using<\/span> System.Web.Http.Filters;<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum8\" style=\"color: #606060;\">   8:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum9\" style=\"color: #606060;\">   9:<\/span> <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span> HarusPakaiHttpsAttribute : AuthorizationFilterAttribute<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum10\" style=\"color: #606060;\">  10:<\/span> {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum11\" style=\"color: #606060;\">  11:<\/span>     <span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">override<\/span> <span style=\"color: #0000ff;\">void<\/span> OnAuthorization(HttpActionContext actionContext)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum12\" style=\"color: #606060;\">  12:<\/span>     {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum13\" style=\"color: #606060;\">  13:<\/span>         <span style=\"color: #0000ff;\">if<\/span> (actionContext.Request.RequestUri.Scheme != Uri.UriSchemeHttps)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum14\" style=\"color: #606060;\">  14:<\/span>         {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum15\" style=\"color: #606060;\">  15:<\/span>             actionContext.Response = <span style=\"color: #0000ff;\">new<\/span> HttpResponseMessage(System.Net.HttpStatusCode.Forbidden)<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum16\" style=\"color: #606060;\">  16:<\/span>             {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum17\" style=\"color: #606060;\">  17:<\/span>                 ReasonPhrase = <span style=\"color: #006080;\">\"HTTPS Required\"<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum18\" style=\"color: #606060;\">  18:<\/span>             };<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum19\" style=\"color: #606060;\">  19:<\/span>         }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum20\" style=\"color: #606060;\">  20:<\/span>         <span style=\"color: #0000ff;\">else<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum21\" style=\"color: #606060;\">  21:<\/span>         {<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum22\" style=\"color: #606060;\">  22:<\/span>             <span style=\"color: #0000ff;\">base<\/span>.OnAuthorization(actionContext);<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum23\" style=\"color: #606060;\">  23:<\/span>         }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum24\" style=\"color: #606060;\">  24:<\/span>     }<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: white; border-style: none; padding: 0px;\"><span id=\"lnum25\" style=\"color: #606060;\">  25:<\/span><\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\"><span id=\"lnum26\" style=\"color: #606060;\">  26:<\/span> }<\/pre>\n<p><!--CRLF--><\/p>\n<\/div>\n<\/div>\n<p>Langkah kedua cukup cukup menambahkan atribut [HarusPakaiHttps] pada method yang ada pada class Web API Controller.\u00a0 Dari nama atribut [HarusPakaiHttps] dapat kita tebak bahwa nama atribut itu sesuai dengan nama class pada baris ke-9.<\/p>\n<p>Nah berikut ini adalah contoh penggunakan atribut pada method :<\/p>\n<div id=\"codeSnippetWrapper\" style=\"overflow: auto; cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<pre id=\"codeSnippet\" style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">[HarusPakaiHttps]\r\n\r\n<span style=\"color: #0000ff;\">public<\/span> IEnumerable&lt;<span style=\"color: #0000ff;\">string<\/span>&gt; Get()\r\n\r\n{    \r\n\r\n   <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #0000ff;\">string<\/span>[] { <span style=\"color: #006080;\">\"value1\"<\/span>, <span style=\"color: #006080;\">\"value2\"<\/span> };\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Dengan memberikan atribut tersebut maka hanya method itu saja yang harus diakses via jaluar HTTPS. Sedangkan method-method lain yang tidak menggunakan atribut tersebut masih dapat diakses lewat jalur HTTP.<\/p>\n<p>Jika ingin semua method yang ada pada class Web API Controller harus diakses via jaluar HTTPS, maka kita dapat tambakan atribut tersebut pada class seperti pada contoh berikut ini :<\/p>\n<div id=\"codeSnippetWrapper\" style=\"overflow: auto; cursor: text; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 97.5%; direction: ltr; text-align: left; margin: 20px 0px 10px; line-height: 12pt; max-height: 200px; background-color: #f4f4f4; border: silver 1px solid; padding: 4px;\">\n<pre id=\"codeSnippet\" style=\"overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; color: black; direction: ltr; text-align: left; margin: 0em; line-height: 12pt; background-color: #f4f4f4; border-style: none; padding: 0px;\">[HarusPakaiHttps]\r\n\r\n<span style=\"color: #0000ff;\">public<\/span> <span style=\"color: #0000ff;\">class<\/span> GeekController : ApiController\r\n\r\n{\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>Jika ingin mecobanya pada IIS Express di Visual Studio 2013, maka jangan lupa aktifkan terlebih dahulu jaluar SSL seperti yang telah diterangkan pada link di atas.<\/p>\n<p>Berikut adalah contoh response yang akan dilihat jika salah satu method pada class Web API Controller diakses bukan pada jalur HTTPS.<\/p>\n<p><a href=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2014\/03\/blog13.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"blog1\" alt=\"blog1\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" src=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2014\/03\/blog1_thumb3.jpg\" border=\"0\" width=\"550\" height=\"289\" \/><\/a><\/p>\n<p>Dan method-method tersebut akan dapat kembali memberikan respon setelah diakses via jaluar HTTPS. Tetapi tentu saja terlebih dahulu kita akan melihat peringatan seperti berikut ini.<\/p>\n<p><a href=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2014\/03\/blog23.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"blog2\" alt=\"blog2\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" src=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2014\/03\/blog2_thumb3.jpg\" border=\"0\" width=\"550\" height=\"266\" \/><\/a><\/p>\n<p>Dan response akan dapat kita lihat setelah mengklik link \u201cContinue to this website (not recommended).<\/p>\n<p>Selamat mencoba <img decoding=\"async\" alt=\"Smile\" style=\"border-style: none;\" src=\"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2014\/03\/wlEmoticon-smile3.png\" class=\"wlEmoticon wlEmoticon-smile\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pada posting sebelumnya telah diterangkan bagaimana menambahkan Web API Controller pada project website ASP.NET Web Form (http:\/\/www.rezafaisal.net\/?p=1049). Pada posting yang lain juga diterangkan bagaimana mengaktifkan SSL pada IIS Express via Visual Studio 2013 http:\/\/www.rezafaisal.net\/?p=1064. Untuk mengamankan Web API yang telah&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1083,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6],"tags":[68,96,108],"class_list":["post-1085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-softwaredev","tag-asp-net","tag-vs-2013","tag-web-api"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"http:\/\/www.rezafaisal.net\/wp-content\/uploads\/2014\/03\/blog2_thumb3.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1sNAL-hv","_links":{"self":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts\/1085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1085"}],"version-history":[{"count":3,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions"}],"predecessor-version":[{"id":1086,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/posts\/1085\/revisions\/1086"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=\/wp\/v2\/media\/1083"}],"wp:attachment":[{"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1085"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.rezafaisal.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}