0% found this document useful (0 votes)
83 views27 pages

Improving On The Applications Behavior Slides

This document discusses improving an application's behavior by handling errors in the API using custom exceptions, adding logging capabilities using Serilog, and authenticating users with JWT tokens. It provides code examples and steps for configuring exception handling middleware, enabling logging, securing API controllers with the [Authorize] attribute, and adding authentication to a Blazor application. The changes allow for cross-cutting concerns like exceptions, logging, and authentication to be addressed, improving the architecture and maintainability of the application.

Uploaded by

Manehcmis Mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views27 pages

Improving On The Applications Behavior Slides

This document discusses improving an application's behavior by handling errors in the API using custom exceptions, adding logging capabilities using Serilog, and authenticating users with JWT tokens. It provides code examples and steps for configuring exception handling middleware, enabling logging, securing API controllers with the [Authorize] attribute, and adding authentication to a Blazor application. The changes allow for cross-cutting concerns like exceptions, logging, and authentication to be addressed, improving the architecture and maintainability of the application.

Uploaded by

Manehcmis Mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Improving the Application’s Behavior

Gill Cleeren
CTO XPIRIT BELGIUM

@gillcleeren www.snowball.be
Overview
Handling errors in the API
Adding logging capabilities
Authenticating users
Handling Errors in the API
Handling Exceptions

Using custom
exceptions Middleware in API
Defined in the Core project Converts the exception for use
Derive from by the client
ApplicationException
public class NotFoundException : ApplicationException
{

public NotFoundException(string name) : base($"{name} is not found")


{

Defining a Custom Exception


ASP.NET Core middleware
- Component
- Plugged into pipeline
- Works on request or response
- Can decide to shortcircuit the request
- Order is important

Configured in Configure() of Startup


Middleware Request Pipeline

Request

Middleware Middleware Middleware


component component component
1 2 3

Response
Writing Custom Middleware

public class RequestCultureMiddleware


{

private readonly RequestDelegate _next;

public RequestCultureMiddleware(RequestDelegate next)


{
_next = next;
}

public async Task InvokeAsync(HttpContext context)

{ ... }
}
Demo
Creating custom exception classes
Throwing exceptions from Core code
Converting exceptions using middleware
Adding Logging Capabilities
Steps to Enable Logging in the Application

Write log Plug in Serilog


Configure logging Configure in Program
statements
Program.cs and
Ilogger<T> in app code AppSettings.json
Logging in ASP.NET Core

Built-in logging API

Provider-based

LogLevel
private readonly ILogger<CreateEventCommandHandler> _logger;

public CreateEventCommandHandler (ILogger<CreateEventCommandHandler> logger)


{
_logger = logger;
}

...

_logger.LogError("Something went wrong…");

Writing Log Information


Log Levels

Trace Information Error

Debug Warning Critical


Adding Serilog

NuGet package

Different log destinations

Structured logging
<PackageReference Include="Serilog" Version="2.10.0" />

<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />

<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />

<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />

NuGet Packages
Demo
Adding Serilog
Configuring the logger
Logging from the application code
Authenticating Users
API authentication
- JWT token
- Header of request
- Different options exist
• ASP.NET Identity
• Identity Server
• External provider
Authentication Flow

API

Authentication

Other API
endpoints

Blazor app
[Authorize]

[Route("api/[controller]")]

[ApiController]

public class EventsController

{ ... }

Securing the API Controllers


Demo
Adding the AccountController
Configuring the application for JWT
tokens
Securing other controllers
Demo
Adding authentication to the Blazor
application
Summary
Cross-cutting concerns added
- Exception handling
- Logging
- Authentication
“It’s great to see all the different building
blocks that have now come together. The
architecture is lending itself well to be
tested and maintained.
This will help us tremendously!”

Bethany
Team Lead at GloboTicket
“This is great, we can now use this as the
foundation for all future projects.”

Bob T. Hickett
Congratulations
on finishing this course!

You might also like