a resource needs authenOcaOon – and if yes, which claims are required – [AllowAnonymous] to skip authoriza9on for an ac9on – emits the 401 status code, if unsuccessful // minimum requirement is successful authentication [Authorize] public DataController : ApiController { [AllowAnonymous] public Data Get() { … } [Authorize(Role = "Foo")] public HttpResponseMessage Delete(int id) { … } }
– Web APIs and clients live in the same domain • typically server-‐rendered AJAX style callbacks – all security se`ngs inherited from web host • Cross-‐Domain – Web APIs and clients live in different domains • na9ve apps (desktop, mobile) • client side JavaScript code (browser) – web API specific security se`ngs
scenarios – username/password authen9ca9on (w/ session tokens) – integra9on with exis9ng infrastructure (e.g. SAML) – OAuth2 style authen9ca9on & authoriza9on (e.g. JWT tokens) – CORS restric9ons for JavaScript-‐based clients • No built-‐in support in ASP.NET Web API – extensibility enabled via message handler infrastructure – Thinktecture.Iden0tyModel authen9ca9on framework
see all requests and responses • Two scopes – global and per-‐route public class MyHandler : DelegatingHandler { protected async override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { // inspect request var response = await base.SendAsync(request, cancellationToken); // inspect response return response; } }
authen9ca9on • Useful to get rid of passwords/keys on the client – no need to store long lived secrets AuthorizaOon: Basic base64(username:password) GET /service/resource/token <session token> GET /service/resource AuthorizaOon: Session <session token>
Service OPTIONS /service Access-‐Control-‐Request-‐Method: POST Origin: hep://server1 Access-‐Control-‐Allow-‐Origin: hep://server1 POST /service
typically around – WS-‐Federa9on / WS-‐Trust – SAML token/protocol • Not directly compaOble with web API world – SOAP toolkit with WS-‐* support needed – SAML tokens quite heavy • Emerging set of standards – OAuth2 framework – JSON Web Tokens (JWT)
Web Applica9on (Client) Resource Owner POST /token Authorization: Basic (client_id:secret) grant_type=refresh_token& refresh_token=xyz Authoriza9on Server
API Resource Owner Client GET /authorize? client_id=nativeapp& redirect_uri=http://localhost/cb& scope=resource& response_type=token& state=123 Authoriza9on Server
API Resource Owner Client Authoriza9on Server POST /token Authorization: Basic (client_id:secret) grant_type=password& scope=resource& user_name=owner& password=password&
very simple security model • Correct handling of SSL is paramount • ASP.NET Web API is a thin abstracOon layer over HTTP • Password-‐based authenOcaOon is an anO-‐pa?ern • OAuth2 becomes the least common denominator technology for cross-‐plagorm development