ASP.Net WebApi Code
AuthorizationFilterAttribute
The easiest way to protect your code is by adding a filter attribute to your controller or method.Below is the code that checks the request's headers and validates the api code from it.
If invalid, returns an Unauthorized response to the client.
Important: Note the using namespaces! It is importing the WebApi namespaces rather than the ASP.NET MVC.
using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http.Filters; namespace QuestoApi.Filters { public class WebApiKeyAuthorisationAttribute : AuthorizationFilterAttribute { private const string HeaderApikeyName = "ApiKey"; //header name public override void OnAuthorization( System.Web.Http.Controllers.HttpActionContext actionContext) { base.OnAuthorization(actionContext); //AJAX frameworks usually send a OPTIONS, below allows the //options to run as usual. if (actionContext.Request.Method == HttpMethod.Options) return; string key = null; if (actionContext.Request.Headers.Contains(HeaderApikeyName)) { key = actionContext.Request .Headers .GetValues(HeaderApikeyName) .FirstOrDefault(); } if (ValidateApiKey(key)) { //If it is valid, no need to do anything //But alternatively, you can set the HttpContext User //to allow your app identify the user who is calling the API //HttpContext.Current.User = GeneratePrincipal(); } else { //Otherwise, return a response now as an Unauthorized actionContext.Response = actionContext .Request .CreateResponse(HttpStatusCode.Unauthorized); } } private bool ValidateApiKey(string key) { //Write here your key validation return key == "valid-key"; } } }
Using on the controller
You can either place the attribute at the class level:[WebApiKeyAuthorisation] public class RegisterQuestController : ApiControllerOr at method level:
[WebApiKeyAuthorisation] public HttpResponseMessage PostUserQuest(RegisterQuestRequest p)
When placing the attribute, that method or controller will be protected requiring the client to always include the api key into the header of the request.
Once the ApiKey is validated, you can optionally access the user details from the
HttpContext.Current.User = GeneratePrincipal();
Adding Header to Sencha Ext Ajax Request
How to use this using Sencha Tounch 2 Ext:Ext.Ajax.request({ url: url, method: 'POST', headers: { 'Content-Type': 'application/json;charset=utf-8', 'Accept': 'application/json', 'QuestoApiKey': 'valid-key' }, params: data, //your data failure: failureCallback, success: successCallback });
What is the usage of the ApiKey?
ReplyDeleteSomeone can steal the api key ,using very easy methods, and do what he want.
The demonstration above is a quick way of locking your api if you need to. To make it secure you'd need to implement your own token renewal and stuff like that.
DeleteThe code above is not production ready at all, but a simple way of prototyping something with reduced effort at server and client side (i.e. compared to oauth, hawk, etc)
Its very informative blog and useful article thank you for sharing with us , keep posting learn
ReplyDeleteDot NET Online Course bangalore
The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
adanaescort01.com - adiyamanescortxx.com - afyonarackiralama.net - aksarayescort.net - antalyaoyunpark.com - aydinescortkiz.com - balikesirescortlar.com - batmanescortlar.com - bitlisescortlar.com - burdurescortlar.com - bursamalaysias.com - diyarbakirambar.com - edirnedespor.com - erzurumyolkosusu.com - eskisehirescortlari.com - gaziantepekspres.org - gebzeescortkiz.com - giresunmaraton.com - hataykoleji.com - ispartakpss.com - karabukteknik.com - kastamonuajans.net - kayserivalisi.com - kilisescort.com - kocaeliescortlar.com - konyaescortlar.com - kutahyaizemlak.com - malatyadataksi.com - manisaescortlar.com - marasatasoyemlak.com - mardinfanatik.com - mersinmoda.com - muglaapart.net - nigdeyapi.com - orduescortt.com - osmaniyeyorum.com - sakaryanur.com - samsunescortlar.com - siirteyatirim.com - sincanoto.com - tekirdagescortlar.com - tokatforum.com - usakbasin.com - vanescortilan.com - yalovadaemlak.com - yozgattanal.com - sanliurfadayim.com - zonguldakescort.com
ReplyDeleteinstagram takipçi satın al
ReplyDeleteinstagram takipçi satın al
takipçi satın al
takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
aşk kitapları
tiktok takipçi satın al
instagram beğeni satın al
youtube abone satın al
twitter takipçi satın al
tiktok beğeni satın al
tiktok izlenme satın al
twitter takipçi satın al
tiktok takipçi satın al
youtube abone satın al
tiktok beğeni satın al
instagram beğeni satın al
trend topic satın al
trend topic satın al
youtube abone satın al
beğeni satın al
tiktok izlenme satın al
sms onay
youtube izlenme satın al
tiktok beğeni satın al
sms onay
sms onay
perde modelleri
instagram takipçi satın al
takipçi satın al
tiktok jeton hilesi
pubg uc satın al
sultanbet
marsbahis
betboo
betboo
betboo
https://www.escortsmate.com - adana escort - adıyaman escort - afyon escort - ağrı escort - aksaray escort - amasya escort - ankara escort - antalya escort - ardahan escort - artvin escort - aydın escort - balıkesir escort - bartın escort - batman escort - bayburt escort - bilecik escort - bingöl escort - bitlis escort - bolu escort - burdur escort - bursa escort - çanakkale escort - çankırı escort - çorum escort - denizli escort - diyarbakır escort - düzce escort - edirne escort - elazığ escort - erzincan escort - erzurum escort - eskişehir escort - gaziantep escort - gebze escort - giresun escort - gümüşhane escort - hakkari escort - hatay escort - ığdır escort - ısparta escort - izmir escort - istanbul escort - karabük escort - karaman escort - kars escort
ReplyDeleteinstagram takipçi satın al
ReplyDeleteucuz takipçi
takipçi satın al
https://takipcikenti.com
https://ucsatinal.org
instagram takipçi satın al
https://perdemodelleri.org
https://yazanadam.com
instagram takipçi satın al
balon perdeler
petek üstü perde
mutfak tül modelleri
kısa perde modelleri
fon perde modelleri
tül perde modelleri
https://atakanmedya.com
https://fatihmedya.com
https://smmpaketleri.com
https://takipcialdim.com
https://yazanadam.com
yasaklı sitelere giriş
aşk kitapları
yabancı şarkılar
sigorta sorgula
https://cozumlec.com
word indir ücretsiz
tiktok jeton hilesi
rastgele görüntülü sohbet
erkek spor ayakkabı
fitness moves
gym workouts
https://marsbahiscasino.org
http://4mcafee.com
http://paydayloansonlineare.com