Book Image

Mastering ASP.NET Web API

By : Mithun Pattankar
Book Image

Mastering ASP.NET Web API

By: Mithun Pattankar

Overview of this book

Microsoft has unified their main web development platforms. This unification will help develop web applications using various pieces of the ASP.NET platform that can be deployed on both Windows and LINUX. With ASP.NET Core (Web API), it will become easier than ever to build secure HTTP services that can be used from any client. Mastering ASP.NET Web API starts with the building blocks of the ASP.NET Core, then gradually moves on to implementing various HTTP routing strategies in the Web API. We then focus on the key components of building applications that employ the Web API, such as Kestrel, Middleware, Filters, Logging, Security, and Entity Framework.Readers will be introduced to take the TDD approach to write test cases along with the new Visual Studio 2017 live unit testing feature. They will also be introduced to integrate with the database using ORMs. Finally, we explore how the Web API can be consumed in a browser as well as by mobile applications by utilizing Angular 4, Ionic and ReactJS. By the end of this book, you will be able to apply best practices to develop complex Web API, consume them in frontend applications and deploy these applications to a modern hosting infrastructure.
Table of Contents (14 chapters)

Protecting web API

APIs Controller classes are the heart of web API applications performing the most essential work of fulfilling business requirements. We need to protect them from unauthorized access.

The web APIs (controllers) are protected using the Authorize attribute that is applied on controller. Until and unless the client calling is identified, the access to the controller action methods are not given.

The following code snippet shows that the Authorize attribute applied to BudgetCategoryController accesses this API endpoint and will result in an unauthorized response (HTTP 401 Status code):

    using Microsoft.AspNetCore.Authorization; 
 
    namespace PersonalBudget.Controllers 
    {  
      [Authorize] 
      [Route("api/[controller]")] 
      public class BudgetCategoryController : Controller 
      { 
        ... 
      } 
    } 

Trying to access this...