Building HTTP APIs with ASP.NET Core

29
#td17 ch Building HTTP APIs with ASP.NET Core Filip Wojcieszyn Tech Lead Cloud Services, Sonova

Transcript of Building HTTP APIs with ASP.NET Core

Page 1: Building HTTP APIs with ASP.NET Core

#td17ch

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

Page 2: Building HTTP APIs with ASP.NET Core

@filip_wojstrathweb.com

sonova.com

Page 3: Building HTTP APIs with ASP.NET Core

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

Page 4: Building HTTP APIs with ASP.NET Core
Page 5: Building HTTP APIs with ASP.NET Core

Agenda

Plan for today Lightweight HTTP APIs Security Documentation Versioning

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

Page 6: Building HTTP APIs with ASP.NET Core

Lightweight HTTP APIs

Page 7: Building HTTP APIs with ASP.NET Core

What would Martin Fowler do?

Page 8: Building HTTP APIs with ASP.NET Core
Page 9: Building HTTP APIs with ASP.NET Core
Page 10: Building HTTP APIs with ASP.NET Core

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

Page 11: Building HTTP APIs with ASP.NET Core

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

Page 12: Building HTTP APIs with ASP.NET Core

Super lightweightWhole API in single C# file

No MVCMinimal footprint

Page 13: Building HTTP APIs with ASP.NET Core

How To Not Set Up an MVC-based API

Page 14: Building HTTP APIs with ASP.NET Core

How To Set Up an MVC API

Page 15: Building HTTP APIs with ASP.NET Core

Security

Page 16: Building HTTP APIs with ASP.NET Core

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

Page 17: Building HTTP APIs with ASP.NET Core
Page 18: Building HTTP APIs with ASP.NET Core

Securing an API (embedded server)

Page 19: Building HTTP APIs with ASP.NET Core

Documentation

Page 20: Building HTTP APIs with ASP.NET Core

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

Page 21: Building HTTP APIs with ASP.NET Core
Page 22: Building HTTP APIs with ASP.NET Core
Page 23: Building HTTP APIs with ASP.NET Core
Page 24: Building HTTP APIs with ASP.NET Core

Versioning

Page 25: Building HTTP APIs with ASP.NET Core

Versioning is difficult

Page 26: Building HTTP APIs with ASP.NET Core

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

Header-basedGET /contactsVersion: 2

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

Page 27: Building HTTP APIs with ASP.NET Core

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

Page 28: Building HTTP APIs with ASP.NET Core

© 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

Page 29: Building HTTP APIs with ASP.NET Core

© 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