ASP.NET Core Samples: 15+ Example Applications in the dotnet/aspnetcore Repository
The samples directory in the dotnet/aspnetcore repository contains self-contained, runnable applications demonstrating authentication patterns, middleware configurations, hosting scenarios, and API styles that developers can execute directly with dotnet run.
The dotnet/aspnetcore repository includes a comprehensive collection of example applications organized by subsystem. These ASP.NET Core samples provide focused implementations of specific framework features, allowing developers to examine working code without scaffolding entire projects. Each sample targets a single scenario—such as cookie authentication, output caching, or Azure deployment—and includes a project file, README, and complete source code.
Security and Authentication Samples
The src/Security/samples folder contains the most extensive collection of ASP.NET Core samples, covering everything from static file protection to dynamic scheme selection.
StaticFilesAuth
StaticFilesAuth demonstrates how to restrict access to static files using authentication and authorization policies. Located in src/Security/samples/StaticFilesAuth, this sample implements two distinct approaches: MapAuthenticatedFiles (allowing any authenticated user) and MapImperativeFiles (enforcing policy-based per-user directories).
The configuration in Startup.cs wires up a sub-pipeline that only serves files after authentication:
// Inside Startup.Configure (lines 88-93)
app.Map("/MapAuthenticatedFiles", branch =>
{
// No policy – any authenticated user can read files
branch.Use((ctx, nxt) => { SetFileEndpoint(ctx, files, null); return nxt(ctx); });
branch.UseAuthorization(); // <-- enforces authentication
SetupFileServer(branch, files);
});
Key files include Startup.cs and Program.cs in the sample root.
PathSchemeSelection
This sample shows how to select between cookie-based and API authentication schemes based on the request path. Using AddPolicyScheme, the application routes /api requests to a bearer token handler while routing standard MVC requests to cookie authentication. The implementation lives in src/Security/samples/PathSchemeSelection/Startup.cs.
Identity.ExternalClaims
Located in src/Security/samples/Identity.ExternalClaims, this example copies static claims (such as gender) and dynamic claims (access tokens) from Google OAuth into the local identity cookie. It highlights the OnCreatingTicket event and claim-mapping techniques for external identity providers.
Additional Security Examples
- DynamicSchemes: Located in
src/Security/samples/DynamicSchemes, this sample demonstrates adding authentication schemes at runtime and selecting them per request. - CustomPolicyProvider: Shows how to implement a custom
IAuthorizationPolicyProviderto generate policies dynamically. SeeCustomPolicyProvider.csinsrc/Security/samples/CustomPolicyProvider. - ClaimsTransformation: Located in
src/Security/samples/ClaimsTransformation, this example usesIClaimsTransformationto add claims to a principal after authentication completes.
Hosting and Deployment Samples
The src/Azure/samples directory contains ASP.NET Core samples specifically targeting cloud deployment scenarios.
AzureAppServicesSample
This sample demonstrates deploying to Azure App Service using UseAzureAppServices and app-service-specific configuration. The setup is contained in src/Azure/samples/AzureAppServicesSample/Startup.cs.
AzureAppServicesHostingStartupSample
Located in the same Azure samples directory, this example implements a hosting startup assembly that runs automatically when the application starts on Azure, illustrating how to inject configuration without modifying the main project code.
Middleware Samples
The src/Middleware directory contains focused examples of ASP.NET Core's middleware pipeline.
Caching Middleware
- ResponseCachingSample: Located in
src/Middleware/ResponseCaching/samples/ResponseCachingSample, this shows how to enable response caching withAddResponseCachingand the[ResponseCache]attribute. - OutputCachingSample: Found in
src/Middleware/OutputCaching/samples/OutputCachingSample, this demonstrates the newer output-caching middleware (AddOutputCache) including Vary-by-query support.
WebSockets and Session
- WebSockets: A simple echo server using
UseWebSocketsand customWebSocketMiddleware, located insrc/Middleware/WebSockets/samples/Startup.cs. - Session: Shows how to configure distributed session state with
AddDistributedMemoryCacheandAddSessioninsrc/Middleware/Session/samples/Startup.cs.
MVC and Minimal API Samples
The repository includes contrasting examples of controller-based and minimalist API approaches.
MvcSample
Located in src/Mvc/samples, this full-featured MVC application demonstrates controllers, views, Razor Pages, routing, model binding, and view components. The configuration is centralized in src/Mvc/samples/Startup.cs.
Minimal API Samples
The src/Http/samples directory contains several ASP.NET Core samples using the minimal API syntax:
- MinimalSample: Shows low-overhead services using
MapGetandMapPostwithout controllers. Seesrc/Http/samples/MinimalSample/Program.cs. - MinimalValidationSample: Demonstrates model validation in minimal APIs using
Validateattributes andIValidator, located insrc/Http/samples/MinimalValidationSample/Program.cs. - MinimalSampleFSharp: The same minimal-API concept expressed in F#, showcasing cross-language support in
src/Http/samples/MinimalSampleFSharp/Program.fs. - MinimalSampleOwin: Shows how to use the OWIN pipeline (
app.Use) inside a minimal API project insrc/Http/samples/MinimalSampleOwin/Program.cs.
Specialized Server and Protocol Samples
Beyond the main categories, several ASP.NET Core samples demonstrate specific server configurations and protocols.
SampleApp (HTTP)
Located in src/Http/samples/SampleApp, this example uses low-level HttpContext manipulation to build requests manually, useful for unit-testing request handling without the full middleware pipeline.
SignalR
The SignalR sample in src/SignalR/samples provides a basic real-time messaging hub with client-side JavaScript, demonstrating persistent connections and message broadcasting.
Server Configuration
- Kestrel Samples: Show how to configure the Kestrel web server, including HTTPS endpoints.
- HttpSys Samples: Demonstrate Windows-specific features like Windows Authentication and kernel-mode caching.
How to Run the Samples
All samples are standard .NET projects that can be executed from the command line. Navigate to the sample directory and run:
cd src/Security/samples/StaticFilesAuth
dotnet run
By default, the application launches on https://localhost:5001. You can then explore the protected routes (such as /MapAuthenticatedFiles or /MapImperativeFiles) as documented in each sample's README.
Summary
- The samples directory in dotnet/aspnetcore contains self-contained applications organized by subsystem (Security, Hosting, Middleware, MVC).
- Security samples like StaticFilesAuth and DynamicSchemes demonstrate authentication patterns using real file paths such as
src/Security/samples/StaticFilesAuth/Startup.cs. - Minimal API samples in
src/Http/samplescontrast with traditional MVC approaches found insrc/Mvc/samples. - All samples are runnable via
dotnet runand include complete project files and documentation. - The repository includes cross-language support with F# examples and platform-specific configurations for Azure and Windows Authentication.
Frequently Asked Questions
Where are the ASP.NET Core samples located in the repository?
The samples are distributed throughout the repository under individual subsystem directories. Security samples reside in src/Security/samples, middleware samples in src/Middleware/<feature>/samples, and minimal API samples in src/Http/samples. Each folder contains a runnable .csproj or .fsproj file with accompanying source code.
How do I run the StaticFilesAuth sample locally?
Navigate to src/Security/samples/StaticFilesAuth and execute dotnet run. The application will start on https://localhost:5001 by default. You can then test the authentication-protected static files by visiting /MapAuthenticatedFiles (requires any authenticated user) or /MapImperativeFiles (requires specific policy authorization).
What is the difference between the MinimalSample and MvcSample?
MvcSample in src/Mvc/samples uses the traditional controller-based architecture with views and Razor Pages, configured through Startup.cs. MinimalSample in src/Http/samples uses the minimal API approach with app.MapGet and app.MapPost directly in Program.cs, eliminating controllers for lower overhead and simpler service scenarios.
Are there F# examples in the ASP.NET Core samples?
Yes, the repository includes MinimalSampleFSharp located in src/Http/samples/MinimalSampleFSharp. This sample demonstrates the minimal API pattern using F# syntax in Program.fs, proving that the framework's core abstractions work across .NET languages.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →