CSC378 ยท 2081 ยท 5 marks
Distinguish between roles and policies. Explain about Kestrel web server in ASP.NET core.
Distinguish between roles and policies. Explain about Kestrel web server in ASP.NET core. [5]
Quick answer
Kestrel is a cross-platform, lightweight web server for ASP.NET Core. It is the web server that is included by default in ASP.NET Core project templates and is built into the ASP.NET Core framework.
Roles vs Policies and Kestrel Web Server in ASP.NET Core
Part 1: Distinguish Between Roles and Policies
| Basis | Roles | Policies |
|---|---|---|
| Definition | Roles are a way to group users based on their job or function (e.g., Admin, User, Manager) | Policies are a set of requirements that must be satisfied for authorization to succeed |
| Approach | Role-based authorization checks if a user belongs to a specific role | Policy-based authorization checks one or more requirements against the current user |
| Flexibility | Less flexible; limited to simple role membership checks | More flexible; can combine multiple requirements (claims, roles, custom logic) |
| Declaration | Declared using [Authorize(Roles = "Admin")] | Declared using [Authorize(Policy = "PolicyName")] |
| Configuration | Roles are assigned directly to users | Policies are defined in Startup.cs using AddAuthorization() |
| Complexity | Suitable for simple access control scenarios | Suitable for complex, fine-grained access control scenarios |
| Example | A user is in the "Admin" role | A policy may require a user to be over 18 AND have a verified email |
Part 2: Kestrel Web Server in ASP.NET Core
Definition
Key Features of Kestrel
- Cross-platform: Kestrel runs on Windows, Linux, and macOS, making ASP.NET Core applications truly cross-platform.
- Default web server: It is automatically included when an ASP.NET Core project is created using the default templates.
- High performance: Kestrel is designed to be fast and efficient for handling HTTP requests.
- Self-hosted: It can run independently without requiring an external web server.
Hosting Models Using Kestrel
There are two ways Kestrel is used in the Out-of-Process Hosting Model:
1. Using Kestrel Directly (Edge Server)
- Kestrel itself acts as an edge server, which directly serves user requests from the internet.
- No additional web server (like IIS or NGINX) is needed.
- Suitable for simpler applications or internal services.
Browser Request --> Kestrel Server --> ASP.NET Core Application
2. Using Kestrel with a Reverse Proxy Server
- Due to limitations of Kestrel, it cannot be used in all scenarios (e.g., sharing a port, advanced security features).
- In such cases, powerful servers like IIS, NGINX, or Apache act as a reverse proxy server.
- The reverse proxy receives the request from the browser and redirects/forwards it to the internal Kestrel server.
- Kestrel then processes the request and returns the response.
Browser Request --> Reverse Proxy (IIS/NGINX/Apache) --> Kestrel Server --> ASP.NET Core App
In-Process Hosting (IIS)
- In this model, the ASP.NET Core app is hosted inside the IIS worker process (
w3wp.exe). - IIS forwards web requests to the backend ASP.NET Core app running the Kestrel server (out-of-process hosting model).
Summary
Kestrel is the heart of ASP.NET Core's web serving capability. It provides a fast, cross-platform, and lightweight server that can either work standalone as an edge server or work behind a powerful reverse proxy like IIS or NGINX for production-grade deployments.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.