Implementing Security in ASP.NET Core: Claims, Patterns, and Policies

Slides:



Advertisements
Similar presentations
Implementing Tableau Server in an Enterprise Environment
Advertisements

Software Testing with Visual Studio 2013 & Team Foundation Server 2013 Benjamin Day.
Team Foundation Server 2010 Builds: Understand, Configure, and Customize Benjamin Day benday.com |
ASP.NET Security MacDonald Ch. 18 MIS 424 MIS 424 Professor Sandvig Professor Sandvig.
Membership, Role Manager and Profile Membership, Role Manager and Profile Matt Gibbs ASP.NET Development Manager.
Jonas Thomsen, Ph.d. student Computer Science University of Aarhus Best Practices and Techniques for Building Secure Microsoft.
Understanding Active Directory
1 ASP.NET SECURITY Presenter: Van Nguyen. 2 Introduction Security is an integral part of any Web-based application. Understanding ASP.NET security will.
Design for Testability: Mocks, Stubs, Refactoring, and User Interfaces Benjamin Day.
SharePoint External Login Access – Forms Authentication vs Azure ACS.
@benday #vslive Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
Doing Something Useful with Enterprise Library 3.0 Benjamin Day Level: Intermediate.
@benday #vslive Automated Build, Test & Deploy with TFS, ASP.NET, and SQL Server Benjamin
Windows Azure Tour Benjamin Day Benjamin Day Consulting, Inc.
IT Unity Webinar Series September 2015 Using Azure Active Directory to Secure Your Apps.
Top 10 Ways to Go from Good to Great Scrum Master Benjamin Day.
SSL, Single Sign On, and External Authentication Presented By Jeff Kelley April 12, 2005.
Goals One ASP.NET Membership story – Web APIs and Web Apps Profile. Extensibility allows for non SQL persistence model. Improve unit testability of.
Team Foundation Server 2012 Builds: Understand, Configure, and Customize Benjamin Day.
Microsoft SharePoint Server 2010 for the Microsoft ASP.NET Developer Yaroslav Pentsarskyy
SQL Server Security By Mattias Lind For PASS Security VC.
Kuali Identity Management Overview. Why did we write KIM? Common Interface for Kuali Applications Provide a Fully-Functional Product A Single API for:
Claims-Based Identity Solution Architect Briefing zoli.herczeg.ro Taken from David Chappel’s work at TechEd Berlin 2009.
Zero to Hero: Untested to Tested with Visual Studio Fakes Benjamin
February, TRANSCEND SHIRO-CAS INTEGRATION ANALYSIS.
Securing Angular Apps Brian Noyes
Real World SQL Server Data Tools Benjamin
Architectural Patterns For The Cloud Brian H. Prince | Microsoft.
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
Benjamin Day Get Good at DevOps: Feature Flag Deployments with ASP.NET, WebAPI, & JavaScript.
Stuff to memorise… "A method tells an object to perform an action. A property allows us to read or change the settings of the object."
Benjamin Unit Testing & Test-Driven Development for Mere Mortals.
Benjamin Day Role-based Security Stinks: Better Authorization in ASP.NET.
Beyond Basic Unit Testing: Mocks, Stubs, User Interfaces, and Refactoring for Testability Benjamin Day
Benjamin Day Real World Scrum with TFS 2015 & VSTS.
SharePoint Authentication and Authorization
DevOps with ASP.NET Core and Entity Framework Core
DYNAMIC CONTENT DELIVERY
Stop Those Prying Eyes Getting to Your Data
Max Fritz Senior Systems Consultant, Now Micro
Introduction to .NET Florin Olariu
Better Unit Tests through Design Patterns: Repository, Adapter, Mocks, and more… Benjamin
CollegeSource Security Application &
ASP .NET MVC Authorization Training Videos
Automate Custom Solutions Deployment on Office 365 and Azure
Cloud SaaS Integrates with Office 365 to Meet the Needs for Business Contact Management “Pobuca combined with Microsoft Office 365 and Azure Active Directory.
6/16/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Using Microsoft Identity Manger with SharePoint 2016 to fill the User Profile Sync Gap Max Fritz Senior Systems Consultant Now Micro.
Building Web Applications with Microsoft ASP
Power BI Security Best Practices
Entity Framework Core for Enterprise Applications
Unit Testing & Test-Driven Development for Mere Mortals
Unit Testing & Test-Driven Development for Mere Mortals
Get Good at DevOps: Feature Flag Deployments with ASP
Windows Identity Foundation Overview
Authorization in Asp.Net Core
Introduction to Authentication Authentication සදහා හැදින්වීම
Purge-it! USP's, pre-sales process & helping the customer to decide
Matthew Levy Azure AD B2B vs B2C Matthew Levy
Stephen W Thomas Using BizTalk Server as your Foundation to the Clouds
Zero to Hero: Untested to Tested with Visual Studio Fakes
Unit Testing & Test-Driven Development for Mere Mortals
Real World Scrum with TFS & VSTS / Azure DevOps
Non-Useless Unit Testing for Entity Framework Core & ASP.NET Core
Entity Framework Core for Enterprise Applications
ASP.NET Authentication with Identity Jump Start
07 | Introduction to Authentication
Presented by Joe Ellers
06 | SQL Server and the Cloud
Presentation transcript:

Implementing Security in ASP.NET Core: Claims, Patterns, and Policies Benjamin Day

Benjamin Day Brookline, MA Consultant & Trainer Rental Chief Technology Officer Therapist for Teams Scrum, DevOps, Azure, Team Foundation Server, Software Architecture & Testing Microsoft MVP Pluralsight Author Scrum.org Trainer @benday

Architecting an ASP.NET Core MVC Application for Unit Testability Just released on 12/31!

On with the show.

Overview Security Overview Security in ASP.NET Core Authentication vs. Authorization Role-based security Claims-based security Security in ASP.NET Core [Authorize] Role-based Policy-based Use the Strategy Pattern to make authorization decisions ASP.NET Core Middleware

Security Overview

Two Big Pieces Authentication Authorization

Two Big Pieces Authentication Authorization Who are you? What can you do? Permissions Two Big Pieces

Authentication Username & passwords in your application Social logins Google, Facebook, Twitter, Microsoft Accounts (MSA) Azure Active Directory (AAD) Windows Active Directory Lots of other options Bad news: Authentication is more complex Good news: It’s usually external to your app Authentication

Authorization Core part of your application logic What is the user allowed to do? User permissions Where are permissions stored? How do you check user permissions in your application logic? Authorization

Claims-based Security Permissions in ASP.NET Role-based Security Claims-based Security

Role-based Security Sample roles: User is a member of a role Administrators Users Power Users Sales Marketing User is a member of a role Application allows roles to do things in the application Role-based Security

Beware: Role-based security has limitations

Role-based Security Concerns Maintenance concerns (Not application security concerns) Fine for simple apps with simple security “Is User X a member of Role Y?” Broad permissions “Is User X a member of Role Y for Item Z?” Permissions in the context of an item Impossible with role-based security Role-based Security Concerns

Claims-based Security Goes beyond role-based security Authorization based on a list of Claims What does the user claim to be? What does the user claim to be able to do? Claims aren’t just permissions Claims can be things like Age Name Email address Roles Claims have context Claims-based Security

Claims Have Context Role-based security is just a role Is the user a member of a role Claims are key/value pairs Claim Type Claim Value “Is User X a member of Role Y for Item Z?” Role-based security can’t do this Claims-based security can Claims Have Context

ASP.NET Core security is primarily about Claims

How is security implemented in ASP.NET Core?

Security in ASP.NET Core IIdentity IPrincipal ClaimsIdentity ClaimsPrincipal

Things to Think About Single Responsibility Principle Code against interfaces Keep logic isolated Dependency Injection Code against IIdentity / IPrincipal ClaimsIdentity / ClaimsPrincipal Things to Think About

You’re focused on testing Authorization Assumption

Two Types of Code Related to Authorization Code that checks if a user is authorized Security decisions Code that you’re trying to authorize Actions you’re trying to protect Keep these separated!!! Single Responsibility Principle Two Types of Code Related to Authorization

It’s always going to be easier to unit test the code that makes the security decisions

If you’re trying to test the decision code and the protected code at the same time…

…it’s probably an integration test and not a unit test

Two Ways to Implement Authorization in ASP.NET Core Using the [Authorize] attribute Part of ASP.NET Core Apply to Controllers or Controller methods Custom logic Checks against IPrincipal yourself Two Ways to Implement Authorization in ASP.NET Core

The [Authorize] Attribute Apply it to a Controller class Controller method User must be authenticated [Authorize()] Role-based authorization [Authorize(Roles = "Administrator")] Policy-based authorization [Authorize(Policy = “AdminOnlyPolicy")] The [Authorize] Attribute

The Bad News About the [Authorize] Attribute Nearly impossible to unit test It’s really hard to integration test My recommendation: Don’t try to test that it’s working Use reflection to check that the attribute is there with the right value(s) The Bad News About the [Authorize] Attribute

Checks Against IPrincipal Get an instance of IPrincipal Write checks against IPrincipal Succeed or fail based on the checks Recommendation: Group the checks into methods that make the authorization decisions Unit test the logic that makes the decisions Checks Against IPrincipal

ASP.NET Core Security Policies Encapsulates the authorization decision logic [Authorize(Policy = “AdminOnlyPolicy")] Define policies in Startup.cs Policy has two parts: Requirement Handler IAuthorizationRequirement AuthorizationHandler<T> ASP.NET Core Security Policies

IAuthorization Requirement Configuration information related to a Policy Create a class Implement the interface (Optional) Provide properties for config values IAuthorization Requirement

Authorization Handler<T> T = Class the implements IAuthorizationRequirement Implement HandleRequirementAsync( context, requirement) AuthorizationHandlerContext Current authorization check info Identity, Principal MVC Context Make the decision Succeed() Fail() Authorization Handler<T>

Testing the Policies Unit test the policy handler logic in isolation Focus testing on AuthorizationHandler<T> Integration testing policy handlers with Controllers is HARD Testing the Policies

Next up: Demos

Unit testing the [Authorize()] attribute “Didn’t you say that was impossible?” Testing the existence of [Authorize()] using Reflection Avoids integration tests Trusts that the decision implementation works Technique is by David Pine

Credit for This Idea David Pine Microsoft MVP Google Developer Expert Twitter: @davidpine7 https://davidpine.net/blog/ asp-net-core-security-unit-testing/ Credit for This Idea

Multi-part demo ASP.NET Core AuthorizationHandler and Policy-based Authorization Part 1: The overall code structure Part 2: Implement the unit tests Part 3: Implement AuthorizationHandler<T> Part 4: Create the policy Hook it in to ASP.NET Core

The President Database is going start selling subscriptions Basic  Search Ultimate  Search + Images Decide if a user is authorized to do something Strategy Pattern Strategy encapsulates algorithms & business logic IUserAuthorizationStrategy DefaultUserAuthorizationStrategy

ASP.NET Core Middleware

What is middleware?

It’s stuff in the middle

Middleware lets you plug your code into the ASP Middleware lets you plug your code into the ASP.NET Core execution pipeline

Middleware https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware

“What does middleware have to do with security?”

Not necessarily anything…

…but it can be really helpful in security scenarios

Use Middleware to hook into the execution pipeline and populate claims

Implementing Middleware Really easy Create a class that implements Microsoft.AspNetCore.Http.IMiddleware InvokeAsync(HttpContext context, RequestDelegate next) Configure Startup.cs Implementing Middleware

ASP.NET Core Middleware Load information about user’s subscription status Populate claims Unit Tests

Any last questions?

Thank you. www.benday.com | benday@benday.com