mdymel / AspNetCoreUtils

Set of utils for ASP.NET Core projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AspNetCoreUtils

AspNetCoreUtils is a library package, which can bring some useful classes into your project. Currently, it's only 4 of them, but I hope to be adding more. You can also contribute if you have an idea for a functionality or maybe have something ready. The project is available on GitHub and as a Nuget package. I want to keep it free of extra dependencies. Currently, the only package it requires is Microsoft.AspNetCore and I would like it to stay that way.

CloudFlareUtilities

If you don't know CloudFlare. Take a moment and check it out now! It's a service, which provides free CDN for your website. You also get DDoS protection and effortless SSL. Really worth to try it out. Because it's a CDN, requests to your website will come from a proxy, so if you want to access your users IP address, you can't simply get it from the HttpContext object. You need to use X-Forwarded headers. CloudFlare provides a list of its network IP addresses. This list is needed to properly configure UseForwardedHeaders option. I described it in detail in another post. CloudFlareUtilities downloads the list (or falls back to a copy), parses it and returns ForwardedHeadersOptions object. So everything you need to do to configure your app is this:

var options = new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.All,
    RequireHeaderSymmetry = false,
    ForwardLimit = null
};
CloudFlareUtilities.FillKnownNetworks(options);
app.UseForwardedHeaders(options);

ForwardedHeadersRegistration

If you want to use a standard configuration for the UseForwardedHeaders with CloudFlare IP addresses, you can use this class to make the registration.

app.RegisterForwardedHeaders(Options.ReverseProxyIp);

Reverse proxy IP is useful for example when you're using Docker behind Nginx server to host your application.

ErrorHandlerMiddleware

That's a middleware allowing you to return an empty response with the desired status code from your API. For example 404 Not Found. If have wrote a post about it too. After you enable it in the Startup class:

app.UseMiddleware<ErrorHandlerMiddleware>();

You can use it like that:

if (sId == null || !ObjectId.TryParse(sId, out var subscriptionId))
{
    throw new ErrorHandlerMiddleware.HttpStatusCodeException(HttpStatusCode.Forbidden);
}

OptionsExtensions

This is a class, which simplifies registration of the application settings stored in the appsettings.json file. You can use this file with IOptions<T> implementation to access your settings. The functionality is described in the documentation. What I didn't like about this was the fact of having a wrapper object over my options, so you'd have to do options.Value.MyProperty. And more importantly, if I was using another assembly with Domain code and wanted to access options, I'd have to reference the ASP assemblies. This is why I prefer to register MyOptions object with the dependency injector. When it's done, I can simply inject MyOptions class whenever I need it. To use it, you need to add this code in your Startup class:

var myOptions = services.RegisterOptions<MyOptions>(Configuration.GetSection("MyOptions"));

This will get the configuration section and register the instance of the MyOptions in the dependency injection container.

About

Set of utils for ASP.NET Core projects

License:GNU General Public License v3.0


Languages

Language:C# 100.0%