{"id":803,"date":"2016-09-16T14:48:55","date_gmt":"2016-09-16T11:48:55","guid":{"rendered":"https:\/\/gokhan-gokalp.com\/?p=803"},"modified":"2016-09-16T14:48:55","modified_gmt":"2016-09-16T11:48:55","slug":"asp-net-web-api-token-based-authentication-refresh-token-kullanimi","status":"publish","type":"post","link":"https:\/\/gokhan-gokalp.com\/tr\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/","title":{"rendered":"Asp.Net Web API &#8211; Token Based Authentication Refresh Token Kullan\u0131m\u0131"},"content":{"rendered":"<p>Merhaba arkada\u015flar.<\/p>\n<p>Biraz aradan sonra tekrar\u00a0bir Asp.Net Web API makalesi ile kar\u015f\u0131n\u0131zday\u0131m. Daha\u00a0\u00f6nceki makalemde\u00a0<em><a href=\"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication\" target=\"_blank\">Asp.Net Web API&#8217;da Token Based Authentication<\/a><\/em> nas\u0131l ger\u00e7ekle\u015ftirilir ve implemente edilir\u00a0konusunu ele\u00a0alm\u0131\u015ft\u0131m. Bu makale kapsam\u0131nda ise sizlerden gelen feedback&#8217;ler do\u011frultusunda ilerleyerek, Token Based Authentication kullan\u0131rken\u00a0<strong>Refresh Token<\/strong>\u00a0nas\u0131l implemente edilir ve nas\u0131l \u00e7al\u0131\u015f\u0131r\u00a0konusunu da ele\u00a0almaya karar verdim.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/token-authentication.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-804 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/token-authentication.png\" alt=\"token-authentication\" width=\"300\" height=\"200\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 300px; --smush-placeholder-aspect-ratio: 300\/200;\" \/><\/a><\/p>\n<h4>Refresh Token Nedir?<\/h4>\n<p>Refresh Token authorization server taraf\u0131ndan mevcutta olan Access Token&#8217;\u0131n expire s\u00fcresi sona ermeye yakla\u015ft\u0131\u011f\u0131nda veya sona erdi\u011finde,\u00a0yeni bir Access Token elde edebilmek\u00a0i\u00e7in client&#8217;a verilir. Refresh Token&#8217;\u0131n kullan\u0131m\u0131 opsiyonel olup, authorization server taraf\u0131ndan Access Token al\u0131naca\u011f\u0131 zaman, Refresh Token da beraberinde verilir.<\/p>\n<blockquote><p><strong>Not<\/strong>:\u00a0Unutulmamal\u0131d\u0131r ki Refresh Token, Access Token&#8217;\u0131n aksine sadece authorization server ile kullan\u0131lmak i\u00e7in tasarlanm\u0131\u015f olup, resource server&#8217;a g\u00f6nderilmemektedir.<\/p><\/blockquote>\n<h4>Refresh Token Nas\u0131l \u00c7al\u0131\u015f\u0131r?<\/h4>\n<p>Dilerseniz Refresh Token&#8217;\u0131n \u00e7al\u0131\u015fma mant\u0131\u011f\u0131na, a\u015fa\u011f\u0131da \u00e7izmeye \u00e7al\u0131\u015ft\u0131\u011f\u0131m\u00a0sequence diyagram\u0131 \u00fczerinden\u00a0bir bakal\u0131m.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/refresh-token-mechanism.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-805 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/refresh-token-mechanism.jpg\" alt=\"refresh-token-mechanism\" width=\"1299\" height=\"1029\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/refresh-token-mechanism.jpg 1299w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/refresh-token-mechanism-300x238.jpg 300w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/refresh-token-mechanism-1024x811.jpg 1024w\" data-sizes=\"(max-width: 1299px) 100vw, 1299px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1299px; --smush-placeholder-aspect-ratio: 1299\/1029;\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Yukar\u0131daki ak\u0131\u015fa bakt\u0131\u011f\u0131m\u0131zda, client \u00f6nce\u00a0authorization yetkilerini alabilmek i\u00e7in authorization server&#8217;a gidiyor ve buradan Access Token&#8217;\u0131 ve Refresh Token&#8217;\u0131 elde ediyor. Daha sonra Access Token ile birlikte resource server&#8217;a eri\u015febiliyor ve istedi\u011fi response&#8217;u alabiliyor. Daha sonra tekrardan ayn\u0131 Access Token bilgisi ile resource server&#8217;a geri geliyor ve bu sefer &#8220;invalid token error&#8221; hatas\u0131 ile kar\u015f\u0131la\u015f\u0131yor. Burada client&#8217;\u0131n Access Token bilgisinin expire oldu\u011funu g\u00f6r\u00fcyoruz. Client ilgili hatan\u0131n ard\u0131ndan daha \u00f6nce elde etmi\u015f oldu\u011fu Refresh Token&#8217;\u0131 kullanarak\u00a0<strong>resource server yerine,<\/strong> direkt olarak authorization server&#8217;a gidiyor ve yeni bir access token ve opsiyonel olarak varsa bir refresh token da elde ediyor. Ak\u0131\u015f bu \u015fekilde devam etmektedir.<\/p>\n<h4>Refresh Token Implementasyonu<\/h4>\n<p>Implementasyon i\u015flemi t\u0131pk\u0131 Token Based Authentication&#8217;da oldu\u011fu gibi gayet basittir. Bu i\u015flemi &#8220;Microsoft.Owin.Security.Infrastructure&#8221; namespace&#8217;i alt\u0131nda bulunan\u00a0<strong>IAuthenticationTokenProvider<\/strong> interface&#8217;ini implemente ederek ger\u00e7ekle\u015ftirece\u011fiz. Implementasyon i\u015flemine ba\u015flamadan \u00f6nce\u00a0<em><a href=\"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication\" target=\"_blank\">Asp.Net Web API&#8217;da Token Based Authentication<\/a>\u00a0<\/em>makalesine\u00a0geri d\u00f6n\u00fcp, makale sonundaki projeyi indirmemiz gerekmektedir. \u00d6rnek projeyi elde etti\u011fimize g\u00f6re, &#8220;OAuth&gt;Providers&#8221; klas\u00f6r\u00fc alt\u0131nda\u00a0<strong>SimpleRefreshTokenProvider<\/strong> isminde yeni bir class ekleyelim.\u00a0<strong>IAuthenticationTokenProvider\u00a0<\/strong>interface&#8217;ini implemente edelim ve\u00a0Refresh Token&#8217;\u0131n olu\u015fturulabilmesini sa\u011flayacak olan &#8220;Create&#8221; method&#8217;unun implementasyonundan a\u015fa\u011f\u0131daki gibi ba\u015flayal\u0131m.<\/p>\n<pre class=\"lang:c# decode:true\">using System.Linq;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.Owin;\r\nusing Microsoft.Owin.Security.Infrastructure;\r\n\r\nnamespace AspNetWebAPIOAuth.OAuth.Providers\r\n{\r\n    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider\r\n    {\r\n        public async Task CreateAsync(AuthenticationTokenCreateContext context)\r\n        {\r\n            Create(context);\r\n        }\r\n\r\n        public void Create(AuthenticationTokenCreateContext context)\r\n        {\r\n            object owinCollection;\r\n            context.OwinContext.Environment.TryGetValue(\"Microsoft.Owin.Form#collection\", out owinCollection);\r\n\r\n            var grantType = ((FormCollection)owinCollection)?.GetValues(\"grant_type\").FirstOrDefault();\r\n\r\n            if (grantType == null || grantType.Equals(\"refresh_token\")) return;\r\n\r\n            \/\/Dilerseniz access_token'dan farkl\u0131 olarak refresh_token'\u0131n expire time'\u0131n\u0131 da belirleyebilir, uzatabilirsiniz \r\n            \/\/context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(1);\r\n\r\n            context.SetToken(context.SerializeTicket());\r\n        }\r\n    }\r\n}<\/pre>\n<p>&#8220;CreateAsync&#8221; method&#8217;u i\u00e7erisinden &#8220;Create&#8221; method&#8217;unu \u00e7a\u011f\u0131r\u0131yoruz ve ayn\u0131 i\u015flemler i\u00e7in tekrardan bir kod tekrar\u0131 i\u015flemi ger\u00e7ekle\u015ftirmiyoruz. &#8220;Create&#8221; method&#8217;una bakt\u0131\u011f\u0131m\u0131zda ise &#8220;OwinContext.Environment&#8221; \u00fczerinden &#8220;Microsoft.Owin.Form#collection&#8221; key&#8217;i ile access_token alabilmek i\u00e7in g\u00f6nderilen parametrelere &#8220;FormCollection&#8221; \u00fczerinden a\u015fa\u011f\u0131daki gibi eri\u015fiyoruz.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/owinCollection.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-809 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/owinCollection.jpg\" alt=\"owincollection\" width=\"641\" height=\"400\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/owinCollection.jpg 641w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/owinCollection-300x187.jpg 300w\" data-sizes=\"(max-width: 641px) 100vw, 641px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 641px; --smush-placeholder-aspect-ratio: 641\/400;\" \/><\/a><\/p>\n<p>Burada &#8220;grant_type&#8221; parametresinin refresh_token olup olmad\u0131\u011f\u0131na bak\u0131yoruz ve bu duruma g\u00f6re context&#8217;e bir token set ediyoruz veya etmiyoruz. Bu kontrol\u00fc yapmam\u0131zdaki sebep ise access_token expire oldu\u011funda veya olmaya yak\u0131n oldu\u011funda client, refresh_token ile server&#8217;a geldi\u011finde tekrardan ge\u00e7erli\u00a0bir access_token daha \u00fcretilirken yeni bir refresh_token daha \u00fcretilmemesi i\u00e7indir. E\u011fer sizler business&#8217;\u0131n\u0131z gere\u011fi her seferinde yeni bir access_token \u00fcretilirken refresh_token&#8217;da \u00fcretilsin istiyorsan\u0131z, bu kontrol\u00fc bu noktada es ge\u00e7ebilirsiniz.<\/p>\n<p>Bu i\u015flemlere ek olarak da \u00fcretilen refresh_token&#8217;\u0131n expire s\u00fcresi, &#8220;Startup.cs&#8221; class&#8217;\u0131 i\u00e7erisinde daha \u00f6nceden <strong>AccessTokenExpireTimeSpan<\/strong> parametresi \u00fczerinden\u00a0access_token i\u00e7in belirlemi\u015f oldu\u011fumuz expire s\u00fcresi ile ayn\u0131 de\u011feri ta\u015f\u0131maktad\u0131r. Bu durumda e\u011fer spesifik olarak herhangi bir de\u011fer atamazsan\u0131z, access_token sona erdi\u011finde refresh_token&#8217;da sona erece\u011fi i\u00e7in yeni bir token \u00fcretilemeyecektir. &#8220;Create&#8221; method&#8217;u i\u00e7erisinde a\u015fa\u011f\u0131daki\u00a0kod par\u00e7ac\u0131\u011f\u0131 ile refresh_token&#8217;\u0131n expire s\u00fcresini spesifik olarak belirleyebilmek m\u00fcmk\u00fcnd\u00fcr.<\/p>\n<pre class=\"lang:c# decode:true \">context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(1);<\/pre>\n<p>Implementasyon i\u015flemine \u015fimdi &#8220;Receive&#8221; method&#8217;u ile devam edece\u011fiz. Bu method ise server&#8217;a refresh_token ile gelindi\u011finde, gerekli kontrolleri yaparak yeni bir access_token \u00fcretilmesinden sorumlu olan method&#8217;dur. Implementasyon i\u015flemini a\u015fa\u011f\u0131daki gibi ger\u00e7ekle\u015ftirelim.<\/p>\n<pre class=\"lang:c# decode:true\">using System.Linq;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.Owin;\r\nusing Microsoft.Owin.Security.Infrastructure;\r\n\r\nnamespace AspNetWebAPIOAuth.OAuth.Providers\r\n{\r\n    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider\r\n    {\r\n        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)\r\n        {\r\n            Receive(context);\r\n        }\r\n\r\n        public void Receive(AuthenticationTokenReceiveContext context)\r\n        {\r\n            context.DeserializeTicket(context.Token);\r\n\r\n            if (context.Ticket == null)\r\n            {\r\n                context.Response.StatusCode = 400;\r\n                context.Response.ContentType = \"application\/json\";\r\n                context.Response.ReasonPhrase = \"invalid token\";\r\n                return;\r\n            }\r\n\r\n            context.SetTicket(context.Ticket);\r\n        }\r\n    }\r\n}<\/pre>\n<p>&#8220;ReceiveAsync&#8221; method&#8217;u i\u00e7erisinden &#8220;Receive&#8221; method&#8217;unu \u00e7a\u011f\u0131r\u0131yoruz ve implementasyon\u00a0i\u015flemlerini burada ger\u00e7ekle\u015ftiriyoruz. Burada &#8220;context.Token&#8221; parametresi ile gelen token&#8217;\u0131, &#8220;context.DeserializeTicket&#8221; method&#8217;u ile token&#8217;\u0131n deserialization i\u015flemini ger\u00e7ekle\u015ftiriyoruz.\u00a0Deserialization i\u015fleminden sonra &#8220;context.Ticket&#8221; \u0131n null kontrol\u00fcn\u00fc ger\u00e7ekle\u015ftirip, context i\u00e7erisine &#8220;SetTicket&#8221; method&#8217;u ile token&#8217;\u0131\u00a0aktar\u0131yoruz. Bu i\u015flemlerin ard\u0131ndan Owin bizim i\u00e7in ge\u00e7erli di\u011fer kontrolleri de sa\u011flayarak, yeni bir access_token \u00fcretimini ger\u00e7ekle\u015ftirecektir. Null kontrol\u00fcnde ise sizlerinde bildi\u011fi \u00fczere ge\u00e7ersiz bir token gelmi\u015f ve deserialization i\u015flemi null olarak ger\u00e7ekle\u015fmi\u015ftir. Bu durumda context \u00fczerine &#8220;context.Ticket&#8221; property&#8217;sini set edemeyece\u011fimiz i\u00e7in i\u00e7erisinde 400 status koduna sahip &#8220;invalid token&#8221; response&#8217;unu geriye d\u00f6n\u00fcyoruz.<\/p>\n<p>Implementasyon i\u015flemi bu kadar \u015fimdi sadece olu\u015fturmu\u015f oldu\u011fumuz bu &#8220;SimpleRefreshTokenProvider&#8221; \u0131n &#8220;Startup.cs&#8221; i\u00e7erisine tan\u0131t\u0131lmas\u0131 kald\u0131. Bu i\u015flemin \u00f6ncesinde ise dilerseniz provider&#8217;\u0131n tam implemente edilmi\u015f bir halini g\u00f6relim.<\/p>\n<pre class=\"lang:c# decode:true \">using System.Linq;\r\nusing System.Threading.Tasks;\r\nusing Microsoft.Owin;\r\nusing Microsoft.Owin.Security.Infrastructure;\r\n\r\nnamespace AspNetWebAPIOAuth.OAuth.Providers\r\n{\r\n    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider\r\n    {\r\n        public async Task CreateAsync(AuthenticationTokenCreateContext context)\r\n        {\r\n            Create(context);\r\n        }\r\n\r\n        public void Create(AuthenticationTokenCreateContext context)\r\n        {\r\n            object owinCollection;\r\n            context.OwinContext.Environment.TryGetValue(\"Microsoft.Owin.Form#collection\", out owinCollection);\r\n\r\n            var grantType = ((FormCollection)owinCollection)?.GetValues(\"grant_type\").FirstOrDefault();\r\n\r\n            if (grantType == null || grantType.Equals(\"refresh_token\")) return;\r\n\r\n            \/\/Dilerseniz access_token'dan farkl\u0131 olarak refresh_token'\u0131n expire time'\u0131n\u0131 da belirleyebilir, uzatabilirsiniz \r\n            \/\/context.Ticket.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(1);\r\n\r\n            context.SetToken(context.SerializeTicket());\r\n        }\r\n\r\n        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)\r\n        {\r\n            Receive(context);\r\n        }\r\n\r\n        public void Receive(AuthenticationTokenReceiveContext context)\r\n        {\r\n            context.DeserializeTicket(context.Token);\r\n\r\n            if (context.Ticket == null)\r\n            {\r\n                context.Response.StatusCode = 400;\r\n                context.Response.ContentType = \"application\/json\";\r\n                context.Response.ReasonPhrase = \"invalid token\";\r\n                return;\r\n            }\r\n\r\n            context.SetTicket(context.Ticket);\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u015eimdi &#8220;Startup.cs&#8221; i a\u00e7al\u0131m ve &#8220;ConfigureOAuth&#8221; method&#8217;u i\u00e7erisinde &#8220;RefreshTokenProvider&#8221; property&#8217;sini a\u015fa\u011f\u0131daki gibi tan\u0131mlayal\u0131m.<\/p>\n<pre class=\"lang:c# decode:true \">OAuthAuthorizationServerOptions oAuthAuthorizationServerOptions = new OAuthAuthorizationServerOptions()\r\n{\r\n    TokenEndpointPath = new Microsoft.Owin.PathString(\"\/token\"), \/\/ token alaca\u011f\u0131m\u0131z path'i belirtiyoruz\r\n    AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(1),\r\n    AllowInsecureHttp = true,\r\n    Provider = new SimpleAuthorizationServerProvider(),\r\n    RefreshTokenProvider = new SimpleRefreshTokenProvider()\r\n};<\/pre>\n<p>Art\u0131k t\u00fcm yap\u0131\u00a0haz\u0131r durumda. Test i\u015flemlerimizi bir \u00f6nceki makalede de oldu\u011fu gibi <strong>Postman<\/strong> \u00fczerinden\u00a0ger\u00e7ekle\u015ftirece\u011fiz. \u00d6rne\u011fimizde access_token 1 dakikal\u0131k bir expire s\u00fcresine sahip olurken,\u00a0refresh_token ise 2 dakikal\u0131k bir s\u00fcreye sahip olacak. Projeyi\u00a0\u00e7al\u0131\u015ft\u0131ral\u0131m\u00a0ve Postman \u00fczerinden &#8220;<em>http:\/\/localhost:55992\/token<\/em>&#8221; URI&#8217;\u0131na a\u015fa\u011f\u0131daki gibi bir POST request olu\u015ftural\u0131m.<\/p>\n<p>Headers\u2019e eklenecek parametreler:<\/p>\n<p><strong>Header<\/strong>: Accept \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<strong>Value<\/strong>: application\/json<br \/>\n<strong>Header<\/strong>: Content-Type \u00a0 \u00a0 <strong>Value<\/strong>: application\/x-www-form-urlencoded<\/p>\n<p>Body k\u0131sm\u0131na eklenecek parametreleri de a\u015fa\u011f\u0131daki gibi set edelim ve POST i\u015flemini ger\u00e7ekle\u015ftirelim.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/access_token_1.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-814 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/access_token_1.jpg\" alt=\"access_token_1\" width=\"973\" height=\"481\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_1.jpg 973w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_1-300x148.jpg 300w\" data-sizes=\"(max-width: 973px) 100vw, 973px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 973px; --smush-placeholder-aspect-ratio: 973\/481;\" \/><\/a><\/p>\n<p>POST i\u015fleminin sonucunda &#8220;access_token&#8221; ve &#8220;refresh_token&#8221; \u0131 elde ettik. &#8220;expires_in&#8221; property&#8217;sinde ise 1 dakikal\u0131k bir s\u00fcreye sahip olabildi\u011fini g\u00f6rebiliyoruz.<\/p>\n<p>&#8220;access_token&#8221; \u0131 kullanarak bir \u00f6nceki makalemizde oldu\u011fu gibi &#8220;Orders&#8221; resource&#8217;unu kullanal\u0131m. Bunun i\u00e7in yine Postman \u00fczerinden &#8220;<em>http:\/\/localhost:55992\/api\/orders\/list<\/em>&#8221; URI&#8217;\u0131na a\u015fa\u011f\u0131daki Headers parametrelerini kullanarak bir GET iste\u011finde bulunal\u0131m ve execute edelim.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/access_token_2.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-815 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/access_token_2.jpg\" alt=\"access_token_2\" width=\"973\" height=\"580\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_2.jpg 973w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_2-300x179.jpg 300w\" data-sizes=\"(max-width: 973px) 100vw, 973px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 973px; --smush-placeholder-aspect-ratio: 973\/580;\" \/><\/a><\/p>\n<p>&#8220;Orders&#8221; resource&#8217;unu access_token ile ba\u015far\u0131l\u0131 bir \u015fekilde consume edebildik. 1 dakikan\u0131n sonunda ise ayn\u0131 access_token ile ayn\u0131 resource&#8217;a tekrardan bir GET iste\u011finde bulunal\u0131m ve response&#8217;a bir bakal\u0131m.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/access_token_3.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-816 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/access_token_3.jpg\" alt=\"access_token_3\" width=\"973\" height=\"560\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_3.jpg 973w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_3-300x173.jpg 300w\" data-sizes=\"(max-width: 973px) 100vw, 973px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 973px; --smush-placeholder-aspect-ratio: 973\/560;\" \/><\/a><\/p>\n<p>Ge\u00e7en 1 dakikan\u0131n ard\u0131ndan g\u00f6rd\u00fc\u011f\u00fcm\u00fcz gibi access_token expire oldu\u011fu i\u00e7in &#8220;Authorization has been denied for this request.&#8221; hatas\u0131n\u0131 ald\u0131k. Hat\u0131rlarsak refresh_token&#8217;\u0131n expire s\u00fcresini ise 2 dakika olarak belirleyece\u011fiz demi\u015ftik. Hemen tekrardan &#8220;<em>http:\/\/localhost:55992\/token<\/em>&#8221; URI&#8217;\u0131na refresh_token&#8217;\u0131 kullanarak a\u015fa\u011f\u0131daki gibi bir POST iste\u011finde bulunal\u0131m.<\/p>\n<p>Headers\u2019e eklenecek parametreler:<\/p>\n<p><strong>Header<\/strong>: Accept \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<strong>Value<\/strong>: application\/json<br \/>\n<strong>Header<\/strong>: Content-Type \u00a0 \u00a0 <strong>Value<\/strong>: application\/x-www-form-urlencoded<\/p>\n<p>Body k\u0131sm\u0131na eklenecek parametreleri de a\u015fa\u011f\u0131daki gibi set edelim ve POST i\u015flemini ger\u00e7ekle\u015ftirelim.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/access_token_4.jpg\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-817 lazyload\" data-src=\"\/wp-content\/uploads\/2016\/09\/access_token_4.jpg\" alt=\"access_token_4\" width=\"973\" height=\"594\" data-srcset=\"https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_4.jpg 973w, https:\/\/gokhan-gokalp.com\/wp-content\/uploads\/2016\/09\/access_token_4-300x183.jpg 300w\" data-sizes=\"(max-width: 973px) 100vw, 973px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 973px; --smush-placeholder-aspect-ratio: 973\/594;\" \/><\/a><\/p>\n<p>Body k\u0131sm\u0131nda ise bu sefer &#8220;grant_type&#8221; \u0131 &#8220;refresh_token&#8221; olarak set ederken, &#8220;refresh_token&#8221; parametresini ise ilk access_token al\u0131rken elde etti\u011fimiz de\u011feri set ettik. Bu i\u015flemlerin sonucunda ise yukar\u0131daki resimde oldu\u011fu gibi yeni bir access_token elde ettik.<\/p>\n<p>Bir konunun daha\u00a0sonuna geldik,\u00a0umar\u0131m herkes i\u00e7in faydal\u0131 bir yaz\u0131 olmu\u015ftur. Ge\u00e7mi\u015f kurban bayram\u0131n\u0131z\u0131 kutlar ve takipte kalman\u0131z\u0131 temenni ederim.<\/p>\n<p>\u00d6rnek uygulamaya a\u015fa\u011f\u0131dan eri\u015febilirsiniz.<\/p>\n<p><a href=\"\/wp-content\/uploads\/2016\/09\/AspNetWebAPIOAuth.rar\">aspnetwebapioauth<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Merhaba arkada\u015flar. Biraz aradan sonra tekrar\u00a0bir Asp.Net Web API makalesi ile kar\u015f\u0131n\u0131zday\u0131m. Daha\u00a0\u00f6nceki makalemde\u00a0Asp.Net Web API&#8217;da Token Based Authentication nas\u0131l ger\u00e7ekle\u015ftirilir ve implemente edilir\u00a0konusunu ele\u00a0alm\u0131\u015ft\u0131m. Bu makale kapsam\u0131nda ise sizlerden gelen feedback&#8217;ler do\u011frultusunda ilerleyerek, Token Based Authentication kullan\u0131rken\u00a0Refresh Token\u00a0nas\u0131l implemente edilir ve nas\u0131l \u00e7al\u0131\u015f\u0131r\u00a0konusunu da&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/gokhan-gokalp.com\/tr\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/\">Devam\u0131n\u0131 okuyun<span class=\"screen-reader-text\">Asp.Net Web API &#8211; Token Based Authentication Refresh Token Kullan\u0131m\u0131<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[116,119,272,275,117,115,112,113,114,271,273,276,118,274],"class_list":["post-803","post","type-post","status-publish","format-standard","hentry","category-asp-net-web-api","tag-access-token","tag-asp-net-web-api-guvenlik","tag-asp-net-web-api-refresh-token","tag-asp-net-web-api-refresh-token-kullanimi","tag-asp-net-web-api-token-based-authentication","tag-bearer-token","tag-oauth","tag-oauth-2-0","tag-owin-oauth","tag-refresh-token","tag-refresh-token-kullanimi","tag-refresh-token-nedir","tag-token-based-authentication","tag-token-based-authentication-refresh-token-kullanimi","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"tr","enabled_languages":["en","tr"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"tr":{"title":false,"content":false,"excerpt":false}}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Asp.Net Web API - Token Based Authentication Refresh Token Kullan\u0131m\u0131 - G\u00f6khan G\u00f6kalp<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Asp.Net Web API - Token Based Authentication Refresh Token Kullan\u0131m\u0131 - G\u00f6khan G\u00f6kalp\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/\" \/>\n<meta property=\"og:site_name\" content=\"G\u00f6khan G\u00f6kalp\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-16T11:48:55+00:00\" \/>\n<meta name=\"author\" content=\"G\u00f6khan G\u00f6kalp\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Yazan:\" \/>\n\t<meta name=\"twitter:data1\" content=\"G\u00f6khan G\u00f6kalp\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/\"},\"author\":{\"name\":\"G\u00f6khan G\u00f6kalp\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\"},\"headline\":\"Asp.Net Web API &#8211; Token Based Authentication Refresh Token Kullan\u0131m\u0131\",\"datePublished\":\"2016-09-16T11:48:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/\"},\"wordCount\":1305,\"commentCount\":20,\"publisher\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\"},\"keywords\":[\"Access Token\",\"Asp.Net Web API G\u00fcvenlik\",\"Asp.Net Web API Refresh Token\",\"Asp.Net Web API Refresh Token Kullan\u0131m\u0131\",\"Asp.net Web API Token Based Authentication\",\"Bearer Token\",\"OAuth\",\"OAuth 2.0\",\"Owin OAuth\",\"Refresh Token\",\"Refresh Token Kullan\u0131m\u0131\",\"Refresh Token Nedir\",\"Token Based Authentication\",\"Token Based Authentication Refresh Token Kullan\u0131m\u0131\"],\"articleSection\":[\"Asp.Net Web API\"],\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/\",\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/\",\"name\":\"Asp.Net Web API - Token Based Authentication Refresh Token Kullan\u0131m\u0131 - G\u00f6khan G\u00f6kalp\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#website\"},\"datePublished\":\"2016-09-16T11:48:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gokhan-gokalp.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Asp.Net Web API &#8211; Token Based Authentication Refresh Token Kullan\u0131m\u0131\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#website\",\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/\",\"name\":\"G\u00f6khan G\u00f6kalp\",\"description\":\"C# &amp; Python lover\",\"publisher\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gokhan-gokalp.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"tr\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/#\\\/schema\\\/person\\\/7e2a7fa98babd22a5fdae563c4b8cdbe\",\"name\":\"G\u00f6khan G\u00f6kalp\",\"pronouns\":\"he\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"tr\",\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325\",\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325\",\"contentUrl\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325\",\"caption\":\"G\u00f6khan G\u00f6kalp\"},\"logo\":{\"@id\":\"https:\\\/\\\/gokhan-gokalp.com\\\/wp-content\\\/litespeed\\\/avatar\\\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325\"},\"sameAs\":[\"https:\\\/\\\/gokhan-gokalp.com\"],\"url\":\"https:\\\/\\\/gokhan-gokalp.com\\\/tr\\\/author\\\/gok-gokalp\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Asp.Net Web API - Token Based Authentication Refresh Token Kullan\u0131m\u0131 - G\u00f6khan G\u00f6kalp","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/","og_locale":"tr_TR","og_type":"article","og_title":"Asp.Net Web API - Token Based Authentication Refresh Token Kullan\u0131m\u0131 - G\u00f6khan G\u00f6kalp","og_url":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/","og_site_name":"G\u00f6khan G\u00f6kalp","article_published_time":"2016-09-16T11:48:55+00:00","author":"G\u00f6khan G\u00f6kalp","twitter_card":"summary_large_image","twitter_misc":{"Yazan:":"G\u00f6khan G\u00f6kalp","Tahmini okuma s\u00fcresi":"8 dakika"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/#article","isPartOf":{"@id":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/"},"author":{"name":"G\u00f6khan G\u00f6kalp","@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe"},"headline":"Asp.Net Web API &#8211; Token Based Authentication Refresh Token Kullan\u0131m\u0131","datePublished":"2016-09-16T11:48:55+00:00","mainEntityOfPage":{"@id":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/"},"wordCount":1305,"commentCount":20,"publisher":{"@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe"},"keywords":["Access Token","Asp.Net Web API G\u00fcvenlik","Asp.Net Web API Refresh Token","Asp.Net Web API Refresh Token Kullan\u0131m\u0131","Asp.net Web API Token Based Authentication","Bearer Token","OAuth","OAuth 2.0","Owin OAuth","Refresh Token","Refresh Token Kullan\u0131m\u0131","Refresh Token Nedir","Token Based Authentication","Token Based Authentication Refresh Token Kullan\u0131m\u0131"],"articleSection":["Asp.Net Web API"],"inLanguage":"tr","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/","url":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/","name":"Asp.Net Web API - Token Based Authentication Refresh Token Kullan\u0131m\u0131 - G\u00f6khan G\u00f6kalp","isPartOf":{"@id":"https:\/\/gokhan-gokalp.com\/#website"},"datePublished":"2016-09-16T11:48:55+00:00","breadcrumb":{"@id":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/#breadcrumb"},"inLanguage":"tr","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gokhan-gokalp.com\/asp-net-web-api-token-based-authentication-refresh-token-kullanimi\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gokhan-gokalp.com\/"},{"@type":"ListItem","position":2,"name":"Asp.Net Web API &#8211; Token Based Authentication Refresh Token Kullan\u0131m\u0131"}]},{"@type":"WebSite","@id":"https:\/\/gokhan-gokalp.com\/#website","url":"https:\/\/gokhan-gokalp.com\/","name":"G\u00f6khan G\u00f6kalp","description":"C# &amp; Python lover","publisher":{"@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gokhan-gokalp.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"tr"},{"@type":["Person","Organization"],"@id":"https:\/\/gokhan-gokalp.com\/#\/schema\/person\/7e2a7fa98babd22a5fdae563c4b8cdbe","name":"G\u00f6khan G\u00f6kalp","pronouns":"he","image":{"@type":"ImageObject","inLanguage":"tr","@id":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325","url":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325","contentUrl":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325","caption":"G\u00f6khan G\u00f6kalp"},"logo":{"@id":"https:\/\/gokhan-gokalp.com\/wp-content\/litespeed\/avatar\/e645f66b6264ced10d7b6d8b1f85509b.jpg?ver=1777985325"},"sameAs":["https:\/\/gokhan-gokalp.com"],"url":"https:\/\/gokhan-gokalp.com\/tr\/author\/gok-gokalp\/"}]}},"_links":{"self":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts\/803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/comments?post=803"}],"version-history":[{"count":5,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts\/803\/revisions"}],"predecessor-version":[{"id":819,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/posts\/803\/revisions\/819"}],"wp:attachment":[{"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/media?parent=803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/categories?post=803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gokhan-gokalp.com\/tr\/wp-json\/wp\/v2\/tags?post=803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}