Skip to content

Informatievlaanderen/correlationid-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Be.Vlaanderen.Basisregisters.AspNetCore.Mvc.Middleware.AddCorrelationId Build Status

Goal

Middleware component which adds a correlation id as a claim for the user on the request context.

Usage

Default claim

namespace Example
{
    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using Be.Vlaanderen.Basisregisters.AspNetCore.Mvc.Middleware;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Hosting;

    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(x => x.UseStartup<Startup>());

            await builder.RunConsoleAsync();
        }
    }

    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app
                .UseMiddleware<AddCorrelationIdMiddleware>()
                .Run(async context => await context.Response.WriteAsync(
                    $"Claims: {string.Join(Environment.NewLine, context.User.Claims.Select(x => $"{x.Type} - {x.Value}"))}"));
        }
    }
}

Running this and making a request to it will result in:

$ curl localhost:5000
Claims: urn:basisregisters:vlaanderen:correlation - 0HM17EA7CUS74:00000001

Custom claim

namespace Example
{
    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using Be.Vlaanderen.Basisregisters.AspNetCore.Mvc.Middleware;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Hosting;

    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(x => x.UseStartup<Startup>());

            await builder.RunConsoleAsync();
        }
    }

    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app
                .UseMiddleware<AddCorrelationIdMiddleware>("urn:example")
                .Run(async context => await context.Response.WriteAsync(
                    $"Claims: {string.Join(Environment.NewLine, context.User.Claims.Select(x => $"{x.Type} - {x.Value}"))}"));
        }
    }
}

Running this and making a request to it will result in:

$ curl localhost:5000
Claims: urn:example - 0HM17EC1IEG3P:00000001

License

MIT License

Credits

Languages & Frameworks

  • .NET Core - MIT
  • .NET Core Runtime - CoreCLR is the runtime for .NET Core. It includes the garbage collector, JIT compiler, primitive data types and low-level classes. - MIT
  • .NET Core APIs - CoreFX is the foundational class libraries for .NET Core. It includes types for collections, file systems, console, JSON, XML, async and many others. - MIT
  • .NET Core SDK - Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI. - MIT
  • Roslyn and C# - The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs. - Apache License 2.0
  • F# - The F# Compiler, Core Library & Tools - MIT
  • F# and .NET Core - F# and .NET Core SDK working together. - MIT

Libraries

  • Paket - A dependency manager for .NET with support for NuGet packages and Git repositories. - MIT
  • FAKE - "FAKE - F# Make" is a cross platform build automation system. - MIT
  • xUnit - xUnit.net is a free, open source, community-focused unit testing tool for the .NET Framework. - Apache License 2.0
  • FluentAssertions - Fluent API for asserting the results of unit tests. - Apache License 2.0

Tooling

Flemish Government Libraries