liveBook · Manning

Chapter 29: Improving your application’s security

What you will learn in chapter 29 of ASP.NET Core in Action, 3e

This chapter introduces common web-application threats and the ASP.NET Core protections and practices used to reduce them, with HTTPS as the essential foundation.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces common web-application threats and the ASP.NET Core protections and practices used to reduce them, with HTTPS as the essential foundation.

Key terms: vulnerabilities, attackers, security practices, HTTPS, encrypt, traffic, cross-site scripting, XSS, cross-site request forgery, CSRF

29.1 Defending against cross-site scripting (XSS) attacks

This section explains how XSS attacks arise from unsafe rendering of user-controlled content and how Razor encoding and context-appropriate output practices prevent them.

Key terms: cross-site scripting, XSS, arbitrary JavaScript, user input, Document Object Model, DOM, HTML encoding, Razor, Tag Helpers, HTML Helpers

29.2 Protecting from cross-site request forgery (CSRF) attacks

This section explains how cookie-authenticated applications are vulnerable to CSRF and how ASP.NET Core antiforgery tokens, data protection keys, and framework-specific validation mitigate that risk.

Key terms: cross-site request forgery, CSRF, cookie authentication, authenticated request, form POST, synchronizer token pattern, antiforgery token, cookie token, __RequestVerificationToken, hidden field

29.3 Calling your web APIs from other domains using CORS

This section introduces the browser same-origin policy and explains how CORS safely permits JavaScript clients on approved origins to call an API on another origin.

Key terms: same-origin policy, origin, scheme, domain, port, cross-origin request, cross-origin resource sharing, CORS, CORS headers, web API

29.3.1 Understanding CORS and how it works

This section explains how CORS policies use response headers and browser preflight checks to control which cross-origin web API requests are permitted.

Key terms: CORS, cross-origin requests, policy, origins, HTTP verbs, credentials, CORS headers, HTTP headers, response, preflight request

29.3.2 Adding a global CORS policy to your whole app

This section shows how to configure and apply a named CORS policy globally so a specified web application can call an API hosted on another origin.

Key terms: CORS, cross-origin requests, API, origin, AddCors(), CORS policy, AllowShoppingApp, WithOrigins(), http://shopping.com, http://api.shopping.com

29.3.3 Adding CORS to specific endpoints with EnableCors metadata

This section shows how to apply named CORS policies selectively to endpoints, route groups, controllers, and actions instead of enabling CORS for an entire application.

Key terms: CORS, cross-origin requests, CORS policy, AddCors, AddPolicy, RequireCors(), [EnableCors], [DisableCors], route group, controller

29.3.4 Configuring CORS policies

This section explains how to configure a least-permissive CORS policy and why cross-origin requests should be avoided unless they are necessary.

Key terms: CORS policy, CorsPolicyBuilder, origins, HTTP verbs, headers, response headers, WithOrigins(), cross-origin requests, cross-origin lockdown, HTTP to HTTPS

29.4 Exploring other attack vectors

This introduction situates XSS and CSRF among OWASP’s critical web-app risks and previews additional common vulnerabilities covered next.

Key terms: XSS, CSRF, OWASP top ten, attack vectors

29.4.1 Detecting and avoiding open redirect attacks

This section explains how unvalidated user-supplied redirect URLs enable open redirect attacks and how to safely restrict redirects to trusted destinations.

Key terms: open redirect attack, returnUrl, query string parameter, Url.IsLocalUrl(), LocalRedirect(), ControllerBase, PageModel, local URL, Identity UI, OpenID Connect

29.4.2 Avoiding SQL injection attacks with EF Core and parameterization

This section explains how SQL injection arises from concatenating untrusted input into queries and how EF Core LINQ queries and parameterized raw SQL prevent it.

Key terms: SQL injection, malicious input, SQL statements, Entity Framework Core, EF Core, object-relational mapper, ORM, LINQ, parameterized queries, FromSqlRaw()

29.4.3 Preventing insecure direct object references

This section explains how predictable URL object IDs can expose unauthorized resources and why endpoint-level resource-based authorization is the required defense.

Key terms: insecure direct object reference, entity, database ID, resource-based authorization, endpoint handlers, permission-denied error, UI, Edit button, GUID, pseudorandom globally unique identifier

29.4.4 Protecting your users’ passwords and data

This section outlines practical minimum practices for minimizing, protecting, and securely handling users’ personal data, passwords, and authentication information.

Key terms: data protection laws, PCI compliance, third-party identity service, ASP.NET Core Identity, cryptographic hashes, BCrypt, PBKDF2, multifactor authentication, MFA, weak passwords

29.5 Summary

This summary recaps how to defend an ASP.NET Core application against common web attacks, control cross-origin API access, and protect user data.

Where this is used later

Concepts explained in this chapter

← Chapter 28: Adding HTTPS to an application Chapter 30: Building ASP.NET Core apps with the generic host and Startup →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.