﻿
{"id":18576,"date":"2020-10-26T17:57:03","date_gmt":"2020-10-26T17:57:03","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=18576"},"modified":"2020-10-26T17:57:03","modified_gmt":"2020-10-26T17:57:03","slug":"identityserver4-yazi-serisi-7-identitymodel-paketi-ile-programatik-yetki-talebinde-bulunma","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/identityserver4-yazi-serisi-7-identitymodel-paketi-ile-programatik-yetki-talebinde-bulunma\/","title":{"rendered":"IdentityServer4 Yaz\u0131 Serisi #7 \u2013 IdentityModel Paketi \u0130le Programatik Yetki Talebinde Bulunma"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/identityserver4-yazi-serisi\/\" rel=\"noopener noreferrer\" target=\"_blank\">IdentityServer4 Yaz\u0131 Serisi<\/a>nin bu yedinci makalesine kadar sunucudan yetki talebini Postman arac\u0131l\u0131\u011f\u0131yla ger\u00e7ekle\u015ftirmi\u015f bulunmaktay\u0131z. Bu i\u00e7eri\u011fimizde ise herhangi bir platformdaki<span style=\"font-size:11px;\">(Console App, Web, Windows Forms vs.)<\/span> uygulama arac\u0131l\u0131\u011f\u0131yla IdentityModel paketi e\u015fli\u011finde Auth Server&#8217;dan nas\u0131l yetki talebinde bulunabilece\u011fimizi inceleyece\u011fiz.<\/p>\n<h4>IdentityModel Paketi Nedir?<\/h4>\n<p>C# ile bir API&#8217;a istekte bulunabilmek i\u00e7in <code>System.Net.Http<\/code> namespace&#8217;i alt\u0131nda bulunan <em>HttpClient<\/em> s\u0131n\u0131f\u0131 kullan\u0131lmaktad\u0131r. Bu s\u0131n\u0131f ile API&#8217;lara yap\u0131lacak olan s\u0131radan istekler \u00e7ok kolay ger\u00e7ekle\u015ftirilebilse de, g\u00f6nderilecek olan request&#8217;in header&#8217;\u0131na parametre eklenmesi gerekti\u011fi durumlarda manuel olarak konfig\u00fcre edilmesi gerekmektedir. Hele hele IdentityServer4 gibi header parametrelerinin yo\u011fun bir \u015fekilde kullan\u0131ld\u0131\u011f\u0131 kompleks yap\u0131lanmalarda <em><a href=\"https:\/\/www.nuget.org\/packages\/IdentityModel\/\" rel=\"noopener noreferrer\" target=\"_blank\">IdentityModel<\/a><\/em> paketi, b\u00fcnyesinde bar\u0131nd\u0131rd\u0131\u011f\u0131 Extension metotlar e\u015fli\u011finde HttpClient s\u0131n\u0131f\u0131n\u0131 zenginle\u015ftirmekte ve header i\u015flemlerini daha dinamik bir \u015fekilde ger\u00e7ekle\u015ftirmemizi sa\u011flamaktad\u0131r. Hatta IdentityModel paketi, IdentityServer4 ile o kadar uyumludur ki, Auth Server&#8217;da bulunan t\u00fcm endpoint&#8217;lere kar\u015f\u0131l\u0131k bir metot bar\u0131nd\u0131rmakta ve b\u00f6ylece bizleri s\u00fcrekli bir endpoint takibinden kurtarmaktad\u0131r. <\/p>\n<h4>IdentityModel E\u015fli\u011finde IdentityServer4&#8217;deki Discovery Endpoint&#8217;i Tetikleme<\/h4>\n<p>IdentityModel sayesinde ilgili Auth Server&#8217;daki Discovery Endpoint&#8217;i &#8216;GetDiscoveryDocumentAsync&#8217; metodu ile tetikleyerek a\u015fa\u011f\u0131daki g\u00f6rselde oldu\u011fu gibi ilgili sunucuya dair t\u00fcm endpoint&#8217;leri elde edebiliriz.<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma.jpg\" alt=\"IdentityServer4 Yaz\u0131 Serisi #7 \u2013 IdentityModel Paketi \u0130le Programatik Yetki Talebinde Bulunma\" width=\"1049\" height=\"692\" class=\"aligncenter size-full wp-image-18584\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma.jpg 1049w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma-300x198.jpg 300w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma-1024x676.jpg 1024w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma-768x507.jpg 768w\" sizes=\"auto, (max-width: 1049px) 100vw, 1049px\" \/><\/a>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere tetikleme neticesinde sunucudaki t\u00fcm endpoint&#8217;ler taraf\u0131m\u0131zca elde edilmi\u015ftir. Haliyle kodlama s\u00fcrecinde endpoint trafi\u011fiyle u\u011fra\u015fmak mecburiyetinde kalmadan direkt olarak operasyona odaklanabilece\u011fiz.<\/p>\n<h4>Programatik Yetki Talebinde Bulunma<\/h4>\n<p>T\u00fcm bu bilgiler e\u015fli\u011finde programatik yetki talebi i\u00e7in a\u015fa\u011f\u0131daki gibi \u00e7al\u0131\u015f\u0131lmas\u0131 yeterlidir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            HttpClient httpClient = new HttpClient();\r\n            DiscoveryDocumentResponse discovery = await httpClient.GetDiscoveryDocumentAsync(&quot;https:\/\/localhost:1000&quot;);\r\n            ClientCredentialsTokenRequest tokenRequest = new ClientCredentialsTokenRequest();\r\n            tokenRequest.ClientId = &quot;GarantiBankasi&quot;;\r\n            tokenRequest.ClientSecret = &quot;garanti&quot;;\r\n            tokenRequest.Address = discovery.TokenEndpoint;\r\n            TokenResponse tokenResponse = await httpClient.RequestClientCredentialsTokenAsync(tokenRequest);\r\n            httpClient.SetBearerToken(tokenResponse.AccessToken);\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu izah etmemiz gerekirse e\u011fer; <strong><em>3.<\/em><\/strong> sat\u0131rda &#8216;ClientCredentialsTokenRequest&#8217; nesnesi ile yetki talep edecek olan client&#8217;\u0131n kimlik bilgilerini ta\u015f\u0131yacak olan nesne tan\u0131mlanmaktad\u0131r. <strong><em>4<\/em><\/strong> ve <strong><em>5.<\/em><\/strong> sat\u0131rlarda client kimlik bilgileri girilmekte, <strong><em>6.<\/em><\/strong> sat\u0131rda ise Auth Server&#8217;\u0131n token endpoint&#8217;i adres olarak bildirilmektedir. Burada dikkat ederseniz token endpoint&#8217;i de manuel yazmakla u\u011fra\u015fm\u0131yor ve direkt &#8216;DiscoveryDocumentResponse&#8217; nesnesinden \u00e7ekiyoruz. <strong><em>7.<\/em><\/strong> sat\u0131rda ise &#8216;RequestClientCredentialsTokenAsync&#8217; metodu ile Auth Server&#8217;a istekte bulunuyor ve elde edilen yetkiyi\/token de\u011ferini <strong><em>8.<\/em><\/strong> sat\u0131rda HttpClient nesnesine &#8216;SetBearerToken&#8217; fonksiyonu arac\u0131l\u0131\u011f\u0131yla veriyoruz. Bu i\u015flemlerden sonra HttpClient nesnesi Auth Server&#8217;dan elde edilen token de\u011ferini kullanarak h\u0131zl\u0131 bir \u015fekilde hedef API&#8217;lara istek g\u00f6nderebilecek yetenekte olacakt\u0131r.<\/p>\n<h4>Programatik Yetkiyi Kullanarak API&#8217;lara \u0130stek G\u00f6nderme<\/h4>\n<p>Son olarak geriye HttpClient nesnesini kullanarak hedef API&#8217;lara a\u015fa\u011f\u0131daki gibi istek g\u00f6ndermek kalmaktad\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n            HttpResponseMessage response = await httpClient.GetAsync(&quot;https:\/\/localhost:2000\/api\/garantibank\/bakiye\/3000&quot;);\r\n            if (response.IsSuccessStatusCode)\r\n            {\r\n                int bakiye = JsonSerializer.Deserialize&lt;int&gt;(await response.Content.ReadAsStringAsync());\r\n                Console.WriteLine($&quot;Hesap bakiyeniz : {bakiye}&quot;);\r\n            }\r\n<\/pre>\n<h4>Kodlar\u0131 Derleyelim ve Test Edelim<\/h4>\n<p>Yapt\u0131\u011f\u0131m\u0131z \u00e7al\u0131\u015fmay\u0131 daha net de\u011ferlendirebilmeniz i\u00e7in b\u00fct\u00fcnsel olarak a\u015fa\u011f\u0131da derleyelim ve ard\u0131ndan IdentityServer4 ve API&#8217;ler ayaktayken uygulamay\u0131 test maksatl\u0131 \u00e7al\u0131\u015ft\u0131ral\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static async Task Main(string&#x5B;] args)\r\n        {\r\n            HttpClient httpClient = new HttpClient();\r\n            DiscoveryDocumentResponse discovery = await httpClient.GetDiscoveryDocumentAsync(&quot;https:\/\/localhost:1000&quot;);\r\n            ClientCredentialsTokenRequest tokenRequest = new ClientCredentialsTokenRequest();\r\n            tokenRequest.ClientId = &quot;GarantiBankasi&quot;;\r\n            tokenRequest.ClientSecret = &quot;garanti&quot;;\r\n            tokenRequest.Address = discovery.TokenEndpoint;\r\n            TokenResponse tokenResponse = await httpClient.RequestClientCredentialsTokenAsync(tokenRequest);\r\n            httpClient.SetBearerToken(tokenResponse.AccessToken);\r\n\r\n            HttpResponseMessage response = await httpClient.GetAsync(&quot;https:\/\/localhost:2000\/api\/garantibank\/bakiye\/3000&quot;);\r\n            if (response.IsSuccessStatusCode)\r\n            {\r\n                int bakiye = JsonSerializer.Deserialize&lt;int&gt;(await response.Content.ReadAsStringAsync());\r\n                Console.WriteLine($&quot;Hesap bakiyeniz : {bakiye}&quot;);\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/10\/IdentityServer4-Yazi-Serisi-7-\u2013-IdentityModel-Paketi-Ile-Programatik-Yetki-Talebinde-Bulunma-1.jpg\" alt=\"IdentityServer4 Yaz\u0131 Serisi #7 \u2013 IdentityModel Paketi \u0130le Programatik Yetki Talebinde Bulunma\" width=\"192\" height=\"38\" class=\"aligncenter size-full wp-image-18592\" \/><\/a><\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, IdentityServer4 Yaz\u0131 Serisinin bu yedinci makalesine kadar sunucudan yetki talebini Postman arac\u0131l\u0131\u011f\u0131yla ger\u00e7ekle\u015ftirmi\u015f bulunmaktay\u0131z. Bu i\u00e7eri\u011fimizde ise herhangi bir platformdaki(Console App, Web, Windows Forms vs.) uygulama arac\u0131l\u0131\u011f\u0131yla IdentityModel paketi e\u015fli\u011finde Auth Server&#8217;dan nas\u0131l&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":18168,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3826],"tags":[3870,3866,3869,3868,3827,3867],"class_list":["post-18576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-identityserver4","tag-httpclient","tag-identitymodel","tag-identitymodel-nedir","tag-identitymodel-paketi-nedir","tag-identityserver4","tag-identityserver4-identitymodel"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/18576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=18576"}],"version-history":[{"count":16,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/18576\/revisions"}],"predecessor-version":[{"id":18594,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/18576\/revisions\/18594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/18168"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=18576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=18576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=18576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}