SlideShare a Scribd company logo
DDD Melbourne 2014 security in ASP.Net Web API 2
DDD Melbourne 2014 security in ASP.Net Web API 2
HTTPS = HTTP over TLS
• Server Authentication
• Integrity protection
• Encryption
• Client Authentication
Server Root Cert
Computer – Trusted Root
Certification Authorities
Server SSL Cert
Computer – Personal
(Must have a private key.
Usually a .pfx file)
Client Private Cert
Current User – Personal
(Must have a private key.
Usually a .pfx file)
X.509 Certificates
• ITU-T Standard for PKI
• Standard formats for
certificates
• Installed in Windows
Certificate Store
Client Public Cert
Computer – Trusted People
(Only public key required.
Usually a .cer file)
Bind SSL certificate to port / host
name
• IIS
• netsh.exe
• httpconfig.exe
• CN should match DNS name
Connect
Send Certificate
Generate session key and
encrypt with public key
http://www.moserware.com/2009/06/first-few-milliseconds-of-https.html
Status: 401 (Unauthorised)
WWW-Authenticate: Scheme realm=“app"
GET /URL/Resource
Authorization: scheme <credential>
Authorisation: basic dXNlcjpwYXNzd29yZA==
makecert -r -n "CN=DevRoot" -pe -sv DevRoot.pvk -cy authority DevRoot.cer
• -r Create a self signed certificate
• -n <X509name> Certificate subject X500 name (eg: CN=Fred Dews)
• -pe Mark generated private key as exportable
• -sv <pvkFile> Subject's PVK file; To be created if not present
• -cy <certType> Certificate types
Package the certificate and the private key
pvk2pfx.exe -pvk DevRoot.pvk -spc DevRoot.cer -pfx DevRoot.pfx
makecert -iv DevRoot.pvk -ic DevRoot.cer -n "CN=site.local" -pe -sv %1.pvk -sky exchange
site.local.cer -eku 1.3.6.1.5.5.7.3.1
• -iv <pvkFile> Issuer's PVK file
• -ic <file> Issuer's certificate file
• -n <X509name> Certificate subject X500 name (eg: CN=Fred Dews)
• -pe Mark generated private key as exportable
• -sv <pvkFile> Subject's PVK file; To be created if not present
• -sky <keytype> Subject key type
• -eku <oid[<,oid>]> Comma separated enhanced key usage OIDs
Environment Dictionary
Stores all of the state necessary for
processing an HTTP request and
response, as well as any relevant
server state.
IDictionary<string, object>
"owin.RequestMethod" : A string
containing the HTTP request method
of the request (e.g., "GET", "POST").
Application Delegate (AppFunc)
This is a function signature which serves
as the primary interface between all
components in an OWIN application.
Func<IDictionary<string, object>,
Task>;
• Your appApplication
•Web API
•SignalR
•Nancy
•ServiceStack
Middleware
• Microsoft.Owin.Host.SystemWeb
• Microsoft.Owin.Host.HttpListener
• Helios
Server
•IIS/ASP.Net
•OwinHost.exe
•Self Host
•IIS
Host
Microsoft’s OWIN Implementation
http://katanaproject.codeplex.com/
Hosts and Servers Implementation
IIS
Self-Hosting
OwinHost.exe
Convenience Classes
OwinContext
OwinRequest
OwinResponse
AppBuilderUseExtensions
Middleware for Common Features
Authentication
CORS
DDD Melbourne 2014 security in ASP.Net Web API 2
Web API Web API
Web API
(+ OWIN Adapter)
Self Host Web Host OWIN
WCF ASP.Net
ASP.Net
(+ OWIN Bridge)
Service / Exe IIS IIS
Hosting v1 Hosting v2
Web API
(+ OWIN Adapter)
OWIN
Process/Host
(+ OWIN Bridge)
No System.Web
dependency
Host Web API 2
OWIN
MessageHandler
(global/per-route)
Authentication Filter Authorization Filter
Host/Framework independent concerns,
E.g. authentication
Web API cross-cutting concerns,
E.g. CORS
authorization
Host
OWIN Server
Middleware 1 Middleware 2 ApplicationClient
DDD Melbourne 2014 security in ASP.Net Web API 2
Windows Authentication
• AD Integrated
• Client and Server are on a domain
• The User is a domain account
<system.web>
<authentication mode="Windows" />
</system.web>
public static IAppBuilder UseWindowsAuthentication(this IAppBuilder app){
object value;
if (app.Properties.TryGetValue("System.Net.HttpListener", out value)){
var listener = value as HttpListener;
if (listener != null){
listener.AuthenticationSchemes =
AuthenticationSchemes.IntegratedWindowsAuthentication;
}
}
return app;
}
Users Clients
Do I trust
this app ?
How can I
securely
communicate ?
API
Who is the user ?
Who is the client ?
What are they
authorised to do ?
DDD Melbourne 2014 security in ASP.Net Web API 2
Authorisation
Server
access token
Scopes: read, write, delete
Alice
(Resource Owner)
App
(Client)
Web API
(Resource Server)
http://tools.ietf.org/html/rfc6749
DDD Melbourne 2014 security in ASP.Net Web API 2
Resource Owner Password Credential Flow
• User gives its credentials to the client.
• The client access the auth server on
behalf of the user with the credentials
• Client can optionally authenticate with
the auth server using Basic
authentication scheme.
• Auth server returns an access token –
typically with a short expiry time
Resource Owner Password Credential Flow
• The client then access the Resource
Server using the access token
Native / Browser based clients
• Credential input is not in the client but in the auth server
• No client authentication, client secret not embedded in a
public device
• Client opens a web view to auth server
• Auth server will show a login page and a
consent screen
• Auth server redirects to the callback URL
(# fragment)
• Client extracts the access token and expiry
• Client uses the access token to access the
resource server
Server based clients
Clients can securely store client secret
and client can authenticate with auth
server
• Client opens a web view to auth server
• Auth server will show a login page and a consent screen
• Auth server only sends a authorisation code and access token is not leaked
• Client now directly posts to the auth server, authenticates itself and sends
the authorisation code
• The auth server responds with the access token. The access token is never
leaked to the browser.
• Access token maybe long lived.
• So far auth server and resource server are
in same trusted subsystem
• Allow users to login using Facebook and
then using the Facebook identity to
access the backend services
• Facebook only does authorisation for
their own backend not your backend
Same Origin Policy in Browsers
• AJAX requests to a different host, port or protocol
will fails
• CORS is a W3C standard that allows cross origin
http requests
• The request itself succeeds but the browser
returns an error
• Supported in modern browsers only, IE 10+
CORS support in Web API
• Install-Package
Microsoft.AspNet.WebApi.Cors
• WebApiConfig.cs –
config.EnableCors();
• Controller.cs –
[EnableCors("origin", "headers", "verbs")]
public class MyController : ApiController
{
}
Request Header
Origin: http://cors.local/
Response Header
Access-Control-Allow-Origin: *
Alternative to OAuth for machine to
machine scenario
• Authentication scheme using HMAC
digest of request and response header
• Server and Client shares a secret key for
the hash
• The key is never is not part of the
headers
• Client hashes the header with secret key
• Server hashes the header with same key
and compares the has
• Useful when SSL cannot be used
Request Header
Authorization: Hawk id="dh37fgj492je",
ts="1353832234", nonce="j4h3g2",
mac="werxhqb98rpaxn39848xrunpaw3489r
uxnpa98w4rxn"
Response Header
Server-Authorization: Hawk
mac="YWojrFVgIjgd+RiPacnDwRcL8VtvcMEz
ahVfOpoLxoA=",
hash="yAF3A3y3uzLvNT2m/nVwsifn1+joCqu
0uNWZS8RSv6Y="
With thanks to our sponsors
THANK YOU !

More Related Content

PDF
The Ultimate Guide to Mobile API Security
PDF
Amazon Web Service - Basics
PPTX
IIS Always-On Services
PDF
Build sites on iis
DOCX
IIS interview questions and answers
PPTX
Automating Attacks Against Office365 - BsidesPDX 2016
PPT
OWIN (Open Web Interface for .NET)
The Ultimate Guide to Mobile API Security
Amazon Web Service - Basics
IIS Always-On Services
Build sites on iis
IIS interview questions and answers
Automating Attacks Against Office365 - BsidesPDX 2016
OWIN (Open Web Interface for .NET)

What's hot (20)

PPTX
Asp.Net Identity
PDF
Embracing HTTP in the era of API’s
PDF
JavaCro'14 - Securing web applications with Spring Security 3 – Fernando Redo...
PPTX
IIS7 For Non IIS PFEs
PDF
SQL Injection and DoS
PPT
IIS-Settings
PDF
What are JSON Web Tokens and Why Should I Care?
PDF
V sphere automation_vlaxa_2017
PDF
Super Fast Application development with Mura CMS
PPTX
Dev Ops on AWS with PowerShell (PowerShell Conference Asia 2016)
PPTX
Ntu workshop : REST, PayPal APIs & Async
PPTX
Externally Testing Modern AD Domains - Arcticcon
PPTX
2009 - NRW Conf: (ASP).NET Membership
PDF
Instant ColdFusion with Vagrant
PPTX
Intro to Coldfusion
PPTX
Hosting a website on IIS Server
PPTX
Sherlock Homepage - A detective story about running large web services (VISUG...
PPTX
ASP.NET Core 1.0
PPTX
Building an API in Node with HapiJS
PDF
REST APIs in the context of single-page applications
Asp.Net Identity
Embracing HTTP in the era of API’s
JavaCro'14 - Securing web applications with Spring Security 3 – Fernando Redo...
IIS7 For Non IIS PFEs
SQL Injection and DoS
IIS-Settings
What are JSON Web Tokens and Why Should I Care?
V sphere automation_vlaxa_2017
Super Fast Application development with Mura CMS
Dev Ops on AWS with PowerShell (PowerShell Conference Asia 2016)
Ntu workshop : REST, PayPal APIs & Async
Externally Testing Modern AD Domains - Arcticcon
2009 - NRW Conf: (ASP).NET Membership
Instant ColdFusion with Vagrant
Intro to Coldfusion
Hosting a website on IIS Server
Sherlock Homepage - A detective story about running large web services (VISUG...
ASP.NET Core 1.0
Building an API in Node with HapiJS
REST APIs in the context of single-page applications
Ad

Similar to DDD Melbourne 2014 security in ASP.Net Web API 2 (20)

PPTX
OAuth with AngularJS and WebAPI - SoCal Code Camp 2015
PPTX
Security overview (grahame)
PPTX
O auth2 with angular js
PPTX
Restful api
PDF
JDD2015: Security in the era of modern applications and services - Bolesław D...
PDF
Secured REST Microservices with Spring Cloud
PPTX
Best Practices in Building an API Security Ecosystem
PDF
When and Why Would I use Oauth2?
PPTX
Building Secure User Interfaces With JWTs (JSON Web Tokens)
PDF
Data Synchronization Patterns in Mobile Application Design
PDF
RESTful services and OAUTH protocol in IoT
PPTX
Api security
PDF
PPTX
Microservices security - jpmc tech fest 2018
PDF
Using Communication and Messaging API in the HTML5 World
PDF
Securing Web Applications with Token Authentication
PDF
RESTful Day 5
KEY
Message in a Bottle
PDF
oauth-for-credentials-security-in-rest-api-access
PPTX
Webinar: Embracing REST APIs through APPSeCONNECT
OAuth with AngularJS and WebAPI - SoCal Code Camp 2015
Security overview (grahame)
O auth2 with angular js
Restful api
JDD2015: Security in the era of modern applications and services - Bolesław D...
Secured REST Microservices with Spring Cloud
Best Practices in Building an API Security Ecosystem
When and Why Would I use Oauth2?
Building Secure User Interfaces With JWTs (JSON Web Tokens)
Data Synchronization Patterns in Mobile Application Design
RESTful services and OAUTH protocol in IoT
Api security
Microservices security - jpmc tech fest 2018
Using Communication and Messaging API in the HTML5 World
Securing Web Applications with Token Authentication
RESTful Day 5
Message in a Bottle
oauth-for-credentials-security-in-rest-api-access
Webinar: Embracing REST APIs through APPSeCONNECT
Ad

More from Pratik Khasnabis (10)

PPTX
Open API (aka Swagger) - DDD by Night May 2020
PPTX
Whats new in .net core 3
PPTX
Containers on Windows
PPTX
Microsoft Azure fundamentals for AWS practitioners
PPTX
Deploying a website in Azure using ARM templates
PPTX
What is .Net Standard
PPTX
Recapping C# 6.0 and A First Look Into C# 7.0
PPTX
Deploy a Website in Azure using ARM Templates
PPTX
Async Programming in C# 5
PPTX
Ddd melbourne 2011 C# async ctp
Open API (aka Swagger) - DDD by Night May 2020
Whats new in .net core 3
Containers on Windows
Microsoft Azure fundamentals for AWS practitioners
Deploying a website in Azure using ARM templates
What is .Net Standard
Recapping C# 6.0 and A First Look Into C# 7.0
Deploy a Website in Azure using ARM Templates
Async Programming in C# 5
Ddd melbourne 2011 C# async ctp

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Electronic commerce courselecture one. Pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
DOCX
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Review of recent advances in non-invasive hemoglobin estimation
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Weekly Chronicles - August'25-Week II
gpt5_lecture_notes_comprehensive_20250812015547.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Programs and apps: productivity, graphics, security and other tools
Machine Learning_overview_presentation.pptx
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
The AUB Centre for AI in Media Proposal.docx

DDD Melbourne 2014 security in ASP.Net Web API 2

  • 3. HTTPS = HTTP over TLS • Server Authentication • Integrity protection • Encryption • Client Authentication Server Root Cert Computer – Trusted Root Certification Authorities Server SSL Cert Computer – Personal (Must have a private key. Usually a .pfx file) Client Private Cert Current User – Personal (Must have a private key. Usually a .pfx file) X.509 Certificates • ITU-T Standard for PKI • Standard formats for certificates • Installed in Windows Certificate Store Client Public Cert Computer – Trusted People (Only public key required. Usually a .cer file)
  • 4. Bind SSL certificate to port / host name • IIS • netsh.exe • httpconfig.exe • CN should match DNS name Connect Send Certificate Generate session key and encrypt with public key http://www.moserware.com/2009/06/first-few-milliseconds-of-https.html
  • 5. Status: 401 (Unauthorised) WWW-Authenticate: Scheme realm=“app" GET /URL/Resource Authorization: scheme <credential> Authorisation: basic dXNlcjpwYXNzd29yZA==
  • 6. makecert -r -n "CN=DevRoot" -pe -sv DevRoot.pvk -cy authority DevRoot.cer • -r Create a self signed certificate • -n <X509name> Certificate subject X500 name (eg: CN=Fred Dews) • -pe Mark generated private key as exportable • -sv <pvkFile> Subject's PVK file; To be created if not present • -cy <certType> Certificate types Package the certificate and the private key pvk2pfx.exe -pvk DevRoot.pvk -spc DevRoot.cer -pfx DevRoot.pfx
  • 7. makecert -iv DevRoot.pvk -ic DevRoot.cer -n "CN=site.local" -pe -sv %1.pvk -sky exchange site.local.cer -eku 1.3.6.1.5.5.7.3.1 • -iv <pvkFile> Issuer's PVK file • -ic <file> Issuer's certificate file • -n <X509name> Certificate subject X500 name (eg: CN=Fred Dews) • -pe Mark generated private key as exportable • -sv <pvkFile> Subject's PVK file; To be created if not present • -sky <keytype> Subject key type • -eku <oid[<,oid>]> Comma separated enhanced key usage OIDs
  • 8. Environment Dictionary Stores all of the state necessary for processing an HTTP request and response, as well as any relevant server state. IDictionary<string, object> "owin.RequestMethod" : A string containing the HTTP request method of the request (e.g., "GET", "POST"). Application Delegate (AppFunc) This is a function signature which serves as the primary interface between all components in an OWIN application. Func<IDictionary<string, object>, Task>; • Your appApplication •Web API •SignalR •Nancy •ServiceStack Middleware • Microsoft.Owin.Host.SystemWeb • Microsoft.Owin.Host.HttpListener • Helios Server •IIS/ASP.Net •OwinHost.exe •Self Host •IIS Host
  • 9. Microsoft’s OWIN Implementation http://katanaproject.codeplex.com/ Hosts and Servers Implementation IIS Self-Hosting OwinHost.exe Convenience Classes OwinContext OwinRequest OwinResponse AppBuilderUseExtensions Middleware for Common Features Authentication CORS
  • 11. Web API Web API Web API (+ OWIN Adapter) Self Host Web Host OWIN WCF ASP.Net ASP.Net (+ OWIN Bridge) Service / Exe IIS IIS Hosting v1 Hosting v2 Web API (+ OWIN Adapter) OWIN Process/Host (+ OWIN Bridge) No System.Web dependency
  • 12. Host Web API 2 OWIN MessageHandler (global/per-route) Authentication Filter Authorization Filter Host/Framework independent concerns, E.g. authentication Web API cross-cutting concerns, E.g. CORS authorization Host OWIN Server Middleware 1 Middleware 2 ApplicationClient
  • 14. Windows Authentication • AD Integrated • Client and Server are on a domain • The User is a domain account <system.web> <authentication mode="Windows" /> </system.web> public static IAppBuilder UseWindowsAuthentication(this IAppBuilder app){ object value; if (app.Properties.TryGetValue("System.Net.HttpListener", out value)){ var listener = value as HttpListener; if (listener != null){ listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication; } } return app; }
  • 15. Users Clients Do I trust this app ? How can I securely communicate ? API Who is the user ? Who is the client ? What are they authorised to do ?
  • 17. Authorisation Server access token Scopes: read, write, delete Alice (Resource Owner) App (Client) Web API (Resource Server) http://tools.ietf.org/html/rfc6749
  • 19. Resource Owner Password Credential Flow • User gives its credentials to the client. • The client access the auth server on behalf of the user with the credentials • Client can optionally authenticate with the auth server using Basic authentication scheme. • Auth server returns an access token – typically with a short expiry time
  • 20. Resource Owner Password Credential Flow • The client then access the Resource Server using the access token
  • 21. Native / Browser based clients • Credential input is not in the client but in the auth server • No client authentication, client secret not embedded in a public device • Client opens a web view to auth server • Auth server will show a login page and a consent screen • Auth server redirects to the callback URL (# fragment) • Client extracts the access token and expiry • Client uses the access token to access the resource server
  • 22. Server based clients Clients can securely store client secret and client can authenticate with auth server • Client opens a web view to auth server • Auth server will show a login page and a consent screen • Auth server only sends a authorisation code and access token is not leaked • Client now directly posts to the auth server, authenticates itself and sends the authorisation code • The auth server responds with the access token. The access token is never leaked to the browser. • Access token maybe long lived.
  • 23. • So far auth server and resource server are in same trusted subsystem • Allow users to login using Facebook and then using the Facebook identity to access the backend services • Facebook only does authorisation for their own backend not your backend
  • 24. Same Origin Policy in Browsers • AJAX requests to a different host, port or protocol will fails • CORS is a W3C standard that allows cross origin http requests • The request itself succeeds but the browser returns an error • Supported in modern browsers only, IE 10+ CORS support in Web API • Install-Package Microsoft.AspNet.WebApi.Cors • WebApiConfig.cs – config.EnableCors(); • Controller.cs – [EnableCors("origin", "headers", "verbs")] public class MyController : ApiController { } Request Header Origin: http://cors.local/ Response Header Access-Control-Allow-Origin: *
  • 25. Alternative to OAuth for machine to machine scenario • Authentication scheme using HMAC digest of request and response header • Server and Client shares a secret key for the hash • The key is never is not part of the headers • Client hashes the header with secret key • Server hashes the header with same key and compares the has • Useful when SSL cannot be used Request Header Authorization: Hawk id="dh37fgj492je", ts="1353832234", nonce="j4h3g2", mac="werxhqb98rpaxn39848xrunpaw3489r uxnpa98w4rxn" Response Header Server-Authorization: Hawk mac="YWojrFVgIjgd+RiPacnDwRcL8VtvcMEz ahVfOpoLxoA=", hash="yAF3A3y3uzLvNT2m/nVwsifn1+joCqu 0uNWZS8RSv6Y="
  • 26. With thanks to our sponsors