A few weeks ago, I have started to work on a new project based on ASP.NET Core. I quickly had to deal with problems I have already solved. Some of them I even described on this blog. I started to copy classes from other projects, but then I realised it would be better to make it opensource and available as a NuGet package. And that’s what I have done. Read on to see what utils are available there at the moment.
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.
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:
If you want to use a standard configuration for the
UseForwardedHeaders with CloudFlare IP addresses, you can use this class to make the registration.
Reverse proxy IP is useful for example when you’re using Docker behind Nginx server to host your application.
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
You can use it like that:
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
This will get the configuration section and register the instance of the
MyOptions in the dependency injection container.
It’s not a lot of functionality for now, but I am sure it will grow. If you have a class, which fits in this set, please contribute it to this project :)