Building HTTP APIs with ASP.NET Core

Post on 14-Feb-2017

260 views 7 download

Transcript of Building HTTP APIs with ASP.NET Core

#td17ch

Building HTTP APIs with ASP.NET Core Filip WojcieszynTech Lead Cloud Services, Sonova

@filip_wojstrathweb.com

sonova.com

Before ASP.NET Core HTTP Handlers ASMX WCF Web Http | WCF REST Starter Kit ASP.NET MVC WCF Web API | ASP.NET Web API

Agenda

Plan for today Lightweight HTTP APIs Security Documentation Versioning

Demos https://github.com/filipw/aspnetcore-api-samples

Lightweight HTTP APIs

What would Martin Fowler do?

Source: https://ptrthomas.wordpress.com/2006/06/06/java-call-stack-from-http-upto-jdbc-as-a-picture/

Lightweight APIs with ASP.NET CorePrefer the IRouter extensionsSuper lightweight - modeled after other platforms - e.g. Node, Go, Swift

Use MVC for more complex backendsDon’t unnecessarily rely on the full framework

When using MVC, pick and choose pieces you needAddMvcCore() method allows you to add only most basic framework services

Super lightweightWhole API in single C# file

No MVCMinimal footprint

How To Not Set Up an MVC-based API

How To Set Up an MVC API

Security

HTTP API SecurityUse JSON Web Tokens (JWT)IdentityServer4 is the most popular OpenID Connect and OAuth 2.0 framework for ASP.NET/ASP.NET Core

Tokens can be issued from an embedded server tooAPI could act both as identity and resource server at the same time

ASP.NET Core introduces “Authorization Policies”Centralized way to define required claim validations

Securing an API (embedded server)

Documentation

HTTP API DocumentationSwagger has become a de facto standardDefines swagger.json descriptor file, that allows easy client generation

Swashbuckle package for ASP.NET CoreCan generate swagger.json for your MVC-based HTTP API

ASP.NET Core MVC exposes IApiExplorer serviceIn case you need to manually traverse the endpoints

Versioning

Versioning is difficult

HTTP API Versioning ApproachesURI-basedGET /v1/contactsGET /contacts?v=1

Header-basedGET /contactsVersion: 2

Media type-basedGET /contactsAccept: application/vnd.contact-v2+json

HTTP API Versioning packagesMicrosoft.AspNetCore.Mvc.VersioningA Microsoft versioning package for ASP.NET Core MVC

WebApiContrib.Core.VersioningA community versioning package for ASP.NET Core MVC

© 2017 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a

commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Place your company Logohere

© 2017 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a

commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Place your company Logohere