asp.net core 1.0 security · asp.net core 1.0 asp.net core 1.0 mvc what's new in security?...

30
ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen [email protected] http://brockallen.com @BrockLAllen

Upload: buikien

Post on 15-Apr-2018

295 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

ASP.NETCore1.0ASP.NETCore1.0MVC

What'snewinSecurity?

[email protected]://brockallen.com

@BrockLAllen

Page 2: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

2@BrockLAllen

Wherearewe?

ASP.NET<=4.5 ASP.NET4.5+Katana ASP.NETCore1.0

System.Web.dllModules&Handlers

ASP.NETWebFormsASP.NETMVC

(Simple)Membership

Page 3: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

3@BrockLAllen

"EmptyWebApplication"

Page 4: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

4@BrockLAllen

Wherearewe?

ASP.NET=<4.5 ASP.NET4.5+Katana ASP.NETCore1.0

"System.Web.dll"Modules&Handlers

ASP.NETWebFormsASP.NETMVC

(Simple)Membership

"System.Web.dll"Modules&Handlers

ASP.NETWebFormsASP.NETMVC

OWIN&Katana

ASP.NETWebAPIASP.NETSignalR

ASP.NETIdentity1/2

Page 5: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

5@BrockLAllen

MiddlewareArchitecture

• Middlewarearelinkedcomponentsthatprocessrequests• Applicationcodetargetingaframework(e.g.WebAPI)

Host

OWINServer

SomeMiddleware

SomeOtherMiddlewareUserAgent Application

Page 6: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

6@BrockLAllen

Wherearewe?

ASP.NET=<4.5 ASP.NET4.5+Katana ASP.NETCore1.0

"System.Web.dll"Modules&Handlers

ASP.NETWebFormsASP.NETMVC

(Simple)Membership

"System.Web.dll"Modules&Handlers

ASP.NETWebFormsASP.NETMVC

OWIN&Katana

ASP.NETWebAPIASP.NETSignalR

ASP.NETIdentity1/2

.NETCoreBYOCLR

Re-designX-PlatInspiredbyOWIN

MVC+WebAPIsASP.NETIdentity3

Page 7: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

7@BrockLAllen

Host

ASP.NETCoreArchitecture

• ASP.NETCoreistheruntime(hostedby.NETCore)• MVCisMicrosoft'sprimaryapplicationframework– combineswebUI&API

.NETCore

ASP.NETCore

Middleware MiddlewareUserAgent MVC

DI

Page 8: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

8@BrockLAllen

SecurityArchitectureinASP.NETCore

• EverythingisbasedonClaimsPrincipal– nomorecustomIPrincipal

• Authenticationisimplementedasmiddleware– cookies– externalauthentication

• Othersecurityrelatedservices– CORS,logging,encoding,anti-forgery

• NewdataprotectionAPI• NewauthorizationAPI

Page 9: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

9@BrockLAllen

Identity&AuthenticationAPIs

• ThenewHttpContext– https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http.Abstractions/HttpContext.cs

• AuthenticationManager– https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs

Page 10: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

10@BrockLAllen

CookieAuthenticationMiddleware

• TriggeredwithHttpContext.Authentication.SignInAsync

app.UseCookieAuthentication(options =>{

options.AuthenticationScheme = "Cookies";options.AutomaticAuthenticate = true;options.AutomaticChallenge = true;

options.LoginPath = new PathString("/account/login");

options.AccessDeniedPath =new PathString("/account/forbidden");

});

Page 11: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

11@BrockLAllen

ClaimsTransformation

• Per-requestmanipulationofprincipal&claims

app.UseClaimsTransformation(user =>{

if (user.Identity.IsAuthenticated){

user.Identities.First().AddClaims(GetAppRoles(user));}

return Task.FromResult(user); });

Page 12: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

12@BrockLAllen

ExternalAuthentication

• TriggeredwithHttpContext.Authentication.ChallengeAsync

app.UseGoogleAuthentication(options =>{

options.AuthenticationScheme = "Google";options.SignInScheme = "Cookies";

options.ClientId = "43…43";options.ClientSecret = "3g…Wo";

});

*turnsexternalidentityautomaticallyintoatrustedapplicationcookie

Page 13: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

13@BrockLAllen

ExternalAuthenticationw/Callback

• HttpContext.Authentication.ChallengeAsync• HttpContext.Authentication.AuthenticateAsync

app.UseCookieAuthentication(options =>{

options.AuthenticationScheme = "Temp";options.AutomaticAuthenticate = false;

});

app.UseGoogleAuthentication(options =>{

options.AuthenticationScheme = "Google";options.SignInScheme = "Temp";

});

Page 14: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

14@BrockLAllen

GenericOAuth2.0Middleware

• Many"social"providersabuseOAuth2.0forlogin– manyincompatibledialects(butsimilar)

• NewgenericOAuth2.0base-middlewaremakesimplementationeasier– https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers

• Communityprovidedmiddleware– LinkedIn,Slack,Spotify,WordPress,Yahoo,Github,Instragram,BattleNet,Dropbox,Paypal,Vimeo…

Page 15: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

15@BrockLAllen

Thewayforward…

Browser

NativeApp

ServerApp"Thing"

WebApp

WebAPI WebAPI

WebAPI

SecurityTokenService

Authentication,SSO,accountlinking,federation,sociallogins…

Page 16: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

16@BrockLAllen

SecurityProtocols

Browser

NativeApp

ServerApp"Thing"

WebApp

WebAPI WebAPI

WebAPI

OpenIDConnect*

OAuth 2.0

OAuth 2.0

OAuth 2.0

OAuth 2.0

OAuth 2.0

OAuth 2.0

SecurityTokenService

*

*

Page 17: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

17@BrockLAllen

OpenIDConnectMiddleware

• Muchimproved– codeflowsupport,finergrainednotifications,cleanup…

app.UseOpenIdConnectAuthentication(options =>{

options.AuthenticationScheme = "OIDC",options.SignInScheme = "Cookies";options.AutomaticAuthenticate = true;

options.Authority = "https://identityserver.io";options.ClientId = "mvc6";

options.Scope.Add("openid");options.Scope.Add("email");

});

Page 18: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

18@BrockLAllen

WebAPIAuthentication

• MiddlewareforJWTaccesstokensbuilt-in– cookiesnotrecommended

app.UseOAuthBearerAuthentication(options =>{

options.Authority = "https://localhost:44300";options.Audience = "my.api";

options.AutomaticAuthenticate = true;options.AutomaticChallenge = true;

});

Page 19: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

19@BrockLAllen

IssuingTokens

• Nobuilt-intokenissuancemiddlewareanymore• MicrosoftrecommendsIdentityServer(metoo)– OpenIDConnectandOAuth2.0tokenservice

http://github.com/identityserver

http://leastprivilege.com/2016/01/11/announcing-identityserver-for-asp-net-5-and-net-core/

Page 20: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

20@BrockLAllen

DataProtection

• Whothought this would be agood idea??

Forgiggles: "https://www.google.com/#q=<machineKey filetype:config"

<system.web><!– copied from google seemed legit --><machineKey decryptionKey="656E7...617365206865726547A5"

validationKey="07C1493415E4405F08...6EF8B1F" /> </system.web>

Page 21: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

21@BrockLAllen

KeyContainerLocations

• OnAzureWebApps(no encryption)– %HOME%\ASP.NET\DataProtection-Keys

• If user profile is loaded (encrypted)– %LOCALAPPDATA%\ASP.NET\DataProtection-Keys

• IIS/no profile (encrypted)– RegistryHKLM

• In-Memory

• Manualconfiguration

<?xml version="1.0"encoding="utf-8"?><key id="eacc6495-83a3-4aaf-ad29-fee164c69963" version="1"><creationDate>2015-05-02T08:20:38.6577127Z</creationDate><activationDate>2015-05-02T08:20:38.6424674Z</activationDate><expirationDate>2015-07-31T08:20:38.6424674Z</expirationDate><descriptor><descriptor><encryption algorithm="AES_256_CBC" /><validation algorithm="HMACSHA256"/><encryptedSecret><encryptedKey xmlns=""><!-- Thiskey is encrypted with WindowsDPAPI.--><value>AQAAANCMnd8BFdERjHoAwE/Cl+sBAA..g==</value></encryptedKey></encryptedSecret></descriptor></descriptor></key>

Page 22: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

22@BrockLAllen

ManualConfiguration

• Webfarmscenarioswillrequireasharedlocation– MightalsoprefercertificateratherthanDPAPI

public void ConfigureServices(IServiceCollection services){

services.AddDataProtection();services.ConfigureDataProtection(configure =>{

configure.PersistKeysToFileSystem(new DirectoryInfo(@"\\server\share\directory\"));

configure.ProtectKeysWithCertificate("thumbprint");configure.SetApplicationName("my application");

});}

Page 23: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

23@BrockLAllen

Authorization

• Completere-write– supportforunauthorized vsforbidden– betterseparationofbusinesscodeandauthorizationlogic– re-usablepolicies– resource/actionbasedauthorization– DIenabled

Page 24: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

24@BrockLAllen

[Authorize]

• Similar syntax– roles stillsupported*

[Authorize]public class HomeController : Controller{

[AllowAnonymous]public IActionResult Index(){

return View();}

[Authorize(Roles = "Sales")]public IActionResult About(){

return View(User);}

}

*…andwhothought thatwouldbeagood idea?

Page 25: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

25@BrockLAllen

Authorizationpolicies

services.ConfigureAuthorization(options =>{

options.AddPolicy("SalesSenior", policy =>{

policy.RequireAuthenticatedUser();policy.RequireClaim("department", "sales");policy.RequireClaim("status", "senior");

});};

[Authorize("SalesSenior")]public IActionResult Manage(){

// stuff}

Startup

Controller

Page 26: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

26@BrockLAllen

Resource-basedAuthorization

Subject ObjectOperation

- clientID- subjectID- scopes

- moreclaims

+DI

- read- write- sendviaemail- ...

- ID- owner

- more properties

+DI

Page 27: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

27@BrockLAllen

Example:Documentresource

public class DocumentAuthorizationHandler : AuthorizationHandler<OperationAuthorizationRequirement, Document>

{public override void Handle(AuthorizationContext context, OperationAuthorizationRequirement operation, Document resource)

{// authorization logic

}}

services.AddTransient<IAuthorizationHandler, DocumentAuthorizationHandler>();

DI

Page 28: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

28@BrockLAllen

Invoking the authorization handler

public class DocumentController : Controller{

private readonly IAuthorizationService _authz;

public DocumentController(IAuthorizationService authz){

_authz = authz;}

public async Task<IActionResult> Update(Document doc){

if (!await _authz.AuthorizeAsync(User, doc, Operations.Update)){

// forbiddenreturn new ChallengeResult();

}

// do stuff}

}

Page 29: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

29@BrockLAllen

Resources

• https://github.com/aspnet– home– security– announcements

• http://docs.asp.net

Page 30: ASP.NET Core 1.0 Security · ASP.NET Core 1.0 ASP.NET Core 1.0 MVC What's new in Security? Brock Allen brockallen@gmail.com  @BrockLAllen

30@BrockLAllen

thankyou!