SlideShare a Scribd company logo
Building APIs with MVC 6 and OAuth
@fekberg
I’m Filip Ekberg
Author. Blogger. Speaker. MS MVP. Xamarin MVP. Geek.
Senior Software Engineer @
ASP.NET 5 OAuth Consuming APIs
Building APIs with MVC 6 and OAuth
• Everything!
• Cross-platform
• Open Source
• Modular design (split into NuGet packages)
• And much more..
• Ctrl + H (Find and Replace) Upgrades
• Until RTM
- anything can be renamed
- anything can be removed
• Side-by-side versions makes it easy (dnvm upgrade)
• Powershell, powershell and more powershell…
$out = (Get-Item -Path "." -Verbose).FullName
$(dnu restore --no-cache --lock --unlock --parallel)
get-childitem -recurse -filter 'project.json' -exclude '*artifacts*', '*Build*', '*Publish*' | Where-Object {
!$_.Directory.FullName.Contains("artifacts")
} | ForEach-Object {
$res = $(cd $_.Directory;$?) -and $(dnu build | Out-Host;$?)
-and $(dnu pack --configuration release --out $outBuildPackages)
if (!$res) {
Write-Error "Build failed!"
Exit 1
}
}
$out = (Get-Item -Path "." -Verbose).FullName
get-childitem -recurse -filter 'project.json' -exclude '*artifacts*', '*Build*', '*Publish*' | Where-Object {
$_.Directory.FullName.Contains("Tests")
} | ForEach-Object {
$(cd $_.Directory;$?)
$testOutput = $(dnx . test | Write-Host)
if ($testOutput -contains "*[FAIL]*") {
Write-Error "Tests failed!"
Exit 1
}
}
• Use your own APIs
• Find pain-points before your customers
• Invite other teams to build something
• Allows you to introduce new tech early
• Up-scale and prepare team for the future
• Mitigating risk
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
Disclaimer
• Don’t rely on a third party for a critical system
• Less headaches for your integrators
• Could be added as an option
Building APIs with MVC 6 and OAuth
• Built by industry experts
• Open Source
• Allows you to use OAuth 2.0 and OpenId Connect
• Lots and lots of examples and help available
https://github.com/IdentityServer/IdentityServer3
Tokens
Authorization Code
Trade code for an
Access Token
Access Token
Lets you access a
given resource
Refresh Token
Lets you keep your
Access Token fresh
Treat your Tokens like
passwords!
Remember, they give you
access to a potential
private resource
• JSON Web Token
• Payload (Claims) include Scopes, User info, etc
• Signed
What happens when you don’t validate
a token?
Build your software to assume tokens
are invalid and expired
Building APIs with MVC 6 and OAuth
Securing the API
Choosing an OAuth Flow
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
What if we
already have
authentication?
Identify this in
pre-authentication
and skip OAuth
login screen
Authenticate
against current
system
Authentication is the
process of ascertaining
that somebody really is
who they claims to be
Authorization refers to
rules that determine who
is allowed to do what. E.g.
Filip may be authorized to
create and delete
databases, while Josh is
only authorized to read.
http://stackoverflow.com/a/6556548/39106
Authentication
login + password
(who you are)
Authorization
permissions
(what you are allowed to do)
http://stackoverflow.com/a/20638421/39106
• More than just “OK you access this resource” (OAuth)
• Authorization (Permissions) + Authentication (Login)
• IdentityServer provides OAuth 2.0 + OpenId
Connect
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
• Client Id
• Secret
• Scope(s)
• Return URL
• Grant type
• Credentials / Authorization Code (Flow dependent)
{
"access_token": "eyJ0eXAiO.....",
"expires_in": 3600,
"token_type": "Bearer",
"refresh_token": "cfba7b409dcbb662216bfc5bba80afbc"
}
GET /api/products HTTP/1.1
Host: localhost:1337
Authorization: Bearer eyJ0eXAiOiJK...
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
[HttpDelete]
[Authorize("write")]
[Route("/accounts/{accountId}/documents/{documentId}")]
public async Task<JsonResult> DeleteAsync(string accountId,
long documentId)
Building APIs with MVC 6 and OAuth
Building APIs with MVC 6 and OAuth
Open Source Go-Live! Cross-Platform
Don’t roll your own
security framework
Read the OAuth 2.0
Specification
Know your flows Authentication
vs Authorization
Leverage Claims
Build on-top of existing
infrastructure
Start with non-mission
critical parts of the
business
http://bit.ly/ddd-oauth
Please support our sponsors
To go into the draw for prizes, please
remember to complete your feedback at:
http://www.dddbrisbane.com/feedback
No feedback = No Prizes!
@fekberg
Thank you,
I’m Filip Ekberg!
Author. Blogger. Speaker. MS MVP. Xamarin MVP. Geek.
Senior Software Engineer @

More Related Content

PDF
2014 database - course 1 - www introduction
PPTX
PPTX
Misconfigured CORS, Why being secure isn't getting easier. AppSec USA 2016
PPTX
CORS - Enable Alfresco for CORS
PDF
Cors kung fu
PDF
Cross-domain requests with CORS
PPT
Php basic for vit university
PDF
CORS and (in)security
2014 database - course 1 - www introduction
Misconfigured CORS, Why being secure isn't getting easier. AppSec USA 2016
CORS - Enable Alfresco for CORS
Cors kung fu
Cross-domain requests with CORS
Php basic for vit university
CORS and (in)security

What's hot (19)

PPT
Breaking The Cross Domain Barrier
PPTX
php (Hypertext Preprocessor)
PPTX
Austin Day of Rest - Introduction
PDF
Basic Introduction About API Web Service
PDF
LAWDI - Rogue Linked Data
PPTX
Web Security - Cookies, Domains and CORS
PDF
An Overview of HTML5 Storage
PDF
Cross site calls with javascript - the right way with CORS
PDF
Design Web Service API by HungerStation
ZIP
Drupal, Android and iPhone
PDF
Exploiting null byte vm
PPTX
In-browser storage and me
PDF
Webinar: Getting Started with Ruby and MongoDB
KEY
Extending Custom Post Types
PPTX
Dev Jumpstart: Building Your First App
PDF
Cross Origin Resource Sharing
PPT
Php security
PDF
Design Summit - Security Roadmap - Keenan Brock, Alberto Bellotti
PDF
Parse
Breaking The Cross Domain Barrier
php (Hypertext Preprocessor)
Austin Day of Rest - Introduction
Basic Introduction About API Web Service
LAWDI - Rogue Linked Data
Web Security - Cookies, Domains and CORS
An Overview of HTML5 Storage
Cross site calls with javascript - the right way with CORS
Design Web Service API by HungerStation
Drupal, Android and iPhone
Exploiting null byte vm
In-browser storage and me
Webinar: Getting Started with Ruby and MongoDB
Extending Custom Post Types
Dev Jumpstart: Building Your First App
Cross Origin Resource Sharing
Php security
Design Summit - Security Roadmap - Keenan Brock, Alberto Bellotti
Parse
Ad

Similar to Building APIs with MVC 6 and OAuth (20)

PPTX
OAuth - Don’t Throw the Baby Out with the Bathwater
PPTX
OAuth with AngularJS and WebAPI - SoCal Code Camp 2015
PDF
I Love APIs 2015: Advanced Crash Course in Apigee Edge Workshop
PPTX
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control - W...
PDF
Lock That Shit Down! Auth Security Patterns for Apps, APIs, and Infra - Sprin...
PDF
Lock That Sh*t Down! Auth Security Patterns for Apps, APIs, and Infra
PPTX
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
PDF
OWASP API Security Top 10 Examples
PDF
APIsecure 2023 - OAuth, OIDC and protecting third-party credentials, Ed Olson...
PDF
PPTX
OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control -...
PPTX
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
PPTX
London Adapt or Die: Securing your APIs the Right Way!
PDF
API Security - OWASP top 10 for APIs + tips for pentesters
PDF
Distributed Identities with OpenID
PDF
Takeaways from API Security Breaches Webinar
PPTX
Api security-eic-prabath
PDF
Lock That Sh*t Down! Auth Security Patterns for Apps, APIs, and Infra - Devne...
PDF
Frans Rosén Keynote at BSides Ahmedabad
PPTX
Developing Apps with Azure AD
OAuth - Don’t Throw the Baby Out with the Bathwater
OAuth with AngularJS and WebAPI - SoCal Code Camp 2015
I Love APIs 2015: Advanced Crash Course in Apigee Edge Workshop
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control - W...
Lock That Shit Down! Auth Security Patterns for Apps, APIs, and Infra - Sprin...
Lock That Sh*t Down! Auth Security Patterns for Apps, APIs, and Infra
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
OWASP API Security Top 10 Examples
APIsecure 2023 - OAuth, OIDC and protecting third-party credentials, Ed Olson...
OAuth-as-a-service - using ASP.NET Web API and Windows Azure Access Control -...
OAuth-as-a-service using ASP.NET Web API and Windows Azure Access Control
London Adapt or Die: Securing your APIs the Right Way!
API Security - OWASP top 10 for APIs + tips for pentesters
Distributed Identities with OpenID
Takeaways from API Security Breaches Webinar
Api security-eic-prabath
Lock That Sh*t Down! Auth Security Patterns for Apps, APIs, and Infra - Devne...
Frans Rosén Keynote at BSides Ahmedabad
Developing Apps with Azure AD
Ad

More from Filip Ekberg (8)

PPTX
C# 8 and Beyond
PDF
The State of C#
PPTX
C# Is The Future
PPTX
No More Deadlocks; Asynchronous Programming in .NET
PPTX
Asynchronous programming from Xamarin Hakcday in Melbourne
PPTX
Asynchronous programming
PPTX
Azure Mobile Services .NET Backend
PPTX
C# 6.0 - What?! C# is being updated?
C# 8 and Beyond
The State of C#
C# Is The Future
No More Deadlocks; Asynchronous Programming in .NET
Asynchronous programming from Xamarin Hakcday in Melbourne
Asynchronous programming
Azure Mobile Services .NET Backend
C# 6.0 - What?! C# is being updated?

Recently uploaded (20)

PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Transform Your Business with a Software ERP System
PDF
System and Network Administration Chapter 2
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
top salesforce developer skills in 2025.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
medical staffing services at VALiNTRY
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms II-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Understanding Forklifts - TECH EHS Solution
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
wealthsignaloriginal-com-DS-text-... (1).pdf
Transform Your Business with a Software ERP System
System and Network Administration Chapter 2
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
top salesforce developer skills in 2025.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
medical staffing services at VALiNTRY
CHAPTER 2 - PM Management and IT Context
Which alternative to Crystal Reports is best for small or large businesses.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

Building APIs with MVC 6 and OAuth

  • 2. @fekberg I’m Filip Ekberg Author. Blogger. Speaker. MS MVP. Xamarin MVP. Geek. Senior Software Engineer @
  • 3. ASP.NET 5 OAuth Consuming APIs
  • 5. • Everything! • Cross-platform • Open Source • Modular design (split into NuGet packages) • And much more..
  • 6. • Ctrl + H (Find and Replace) Upgrades • Until RTM - anything can be renamed - anything can be removed • Side-by-side versions makes it easy (dnvm upgrade)
  • 7. • Powershell, powershell and more powershell… $out = (Get-Item -Path "." -Verbose).FullName $(dnu restore --no-cache --lock --unlock --parallel) get-childitem -recurse -filter 'project.json' -exclude '*artifacts*', '*Build*', '*Publish*' | Where-Object { !$_.Directory.FullName.Contains("artifacts") } | ForEach-Object { $res = $(cd $_.Directory;$?) -and $(dnu build | Out-Host;$?) -and $(dnu pack --configuration release --out $outBuildPackages) if (!$res) { Write-Error "Build failed!" Exit 1 } } $out = (Get-Item -Path "." -Verbose).FullName get-childitem -recurse -filter 'project.json' -exclude '*artifacts*', '*Build*', '*Publish*' | Where-Object { $_.Directory.FullName.Contains("Tests") } | ForEach-Object { $(cd $_.Directory;$?) $testOutput = $(dnx . test | Write-Host) if ($testOutput -contains "*[FAIL]*") { Write-Error "Tests failed!" Exit 1 } }
  • 8. • Use your own APIs • Find pain-points before your customers • Invite other teams to build something
  • 9. • Allows you to introduce new tech early • Up-scale and prepare team for the future • Mitigating risk
  • 13. • Don’t rely on a third party for a critical system • Less headaches for your integrators • Could be added as an option
  • 15. • Built by industry experts • Open Source • Allows you to use OAuth 2.0 and OpenId Connect • Lots and lots of examples and help available https://github.com/IdentityServer/IdentityServer3
  • 17. Authorization Code Trade code for an Access Token Access Token Lets you access a given resource Refresh Token Lets you keep your Access Token fresh
  • 18. Treat your Tokens like passwords! Remember, they give you access to a potential private resource
  • 19. • JSON Web Token • Payload (Claims) include Scopes, User info, etc • Signed
  • 20. What happens when you don’t validate a token?
  • 21. Build your software to assume tokens are invalid and expired
  • 28. What if we already have authentication? Identify this in pre-authentication and skip OAuth login screen Authenticate against current system
  • 29. Authentication is the process of ascertaining that somebody really is who they claims to be Authorization refers to rules that determine who is allowed to do what. E.g. Filip may be authorized to create and delete databases, while Josh is only authorized to read. http://stackoverflow.com/a/6556548/39106
  • 30. Authentication login + password (who you are) Authorization permissions (what you are allowed to do) http://stackoverflow.com/a/20638421/39106
  • 31. • More than just “OK you access this resource” (OAuth) • Authorization (Permissions) + Authentication (Login) • IdentityServer provides OAuth 2.0 + OpenId Connect
  • 35. • Client Id • Secret • Scope(s) • Return URL • Grant type • Credentials / Authorization Code (Flow dependent)
  • 36. { "access_token": "eyJ0eXAiO.....", "expires_in": 3600, "token_type": "Bearer", "refresh_token": "cfba7b409dcbb662216bfc5bba80afbc" }
  • 37. GET /api/products HTTP/1.1 Host: localhost:1337 Authorization: Bearer eyJ0eXAiOiJK...
  • 43. Open Source Go-Live! Cross-Platform
  • 44. Don’t roll your own security framework Read the OAuth 2.0 Specification
  • 45. Know your flows Authentication vs Authorization Leverage Claims
  • 46. Build on-top of existing infrastructure Start with non-mission critical parts of the business
  • 48. Please support our sponsors
  • 49. To go into the draw for prizes, please remember to complete your feedback at: http://www.dddbrisbane.com/feedback No feedback = No Prizes!
  • 50. @fekberg Thank you, I’m Filip Ekberg! Author. Blogger. Speaker. MS MVP. Xamarin MVP. Geek. Senior Software Engineer @

Editor's Notes

  • #4: In this talk we’ll go through a lot of content that will help you build a powerful and hopefully more secure API. We’ll start off by talking about ASP.NET 5 for those of you that need to freshen your knowledge, and then we are going to discuss how we can secure this API by introducing OAuth. Of course, we will also talk about how we can consume the API in different scenarios. If you got any questions, or objections during the talk, please feel free to interrupt me!
  • #5: ASP.NET 5, the hot-topic of 2015! It’s fair to say that over the past 12 months, we’ve seen so many good changes coming from Microsoft in terms of open source, hardware, frameworks and software releases that it’s really hard to keep up. ASP.NET 5 is one of these amazing things Microsoft have been working on, and they’ve done this in the open. Everything is open source and freely available on github – you can even help out if you are so inclined! If you’re coming from an earlier version of ASP.NET, a lot of it will look similar, if not the same, don’t let that fool you though, it’s completely re-written and it’s now leveraging a modular architecture that allows you to really pick and choose what parts you want to include in your software.
  • #11: Show how to build an API with ASP.NET 5, include some of the fundamentals -- 15 minutes to this slide --
  • #12: Now that we have an API, it lets us retrieve the data we want – how do we lock this down and make it secure? We want to avoid introducing something custom built that no one will know about, it’s much better if we can adhere to a specification, such as OAuth 2.0. While I introduced Oauth, I found myself becoming best friends with the specification, at least we had a love-hate relationship. More than once I got home from work with a bit of a headache – it’s a lot of interesting concepts and processes to keep in your head at all times! So, we now want to lock down our API by introducing a bit of security. The idea here is that we’ll use something that people consuming our API will be comfortable using. This is where OAuth comes into the picture.
  • #13: Before we start talking about OAuth, security and all those really fun topics – I just want to say that I am by no means a security expert. I’m leveraging as much as possible from what industry experts have already created, and I limit the amount of customization to avoid introducing security holes. If you are working on a critical piece of software that is core to your business, it’s always worth consulting a security expert before going live and doing so on a regular basis. It’s been proven over and over again that even the largest companies with some of the smartest people in the world keep doing small mistakes that can trash their entire reputation. With that out of the way, let’s talk about how we can tighten the security of our API!
  • #23: Show JWT.io
  • #33: Show how to enable IdentityServer on the API that we built in the first demo. Start off with the In-Memory examples and elaborate into a customized solution