Edi.AspNetCore.Utils
1.8.0
dotnet add package Edi.AspNetCore.Utils --version 1.8.0
NuGet\Install-Package Edi.AspNetCore.Utils -Version 1.8.0
<PackageReference Include="Edi.AspNetCore.Utils" Version="1.8.0" />
<PackageVersion Include="Edi.AspNetCore.Utils" Version="1.8.0" />
<PackageReference Include="Edi.AspNetCore.Utils" />
paket add Edi.AspNetCore.Utils --version 1.8.0
#r "nuget: Edi.AspNetCore.Utils, 1.8.0"
#:package Edi.AspNetCore.Utils@1.8.0
#addin nuget:?package=Edi.AspNetCore.Utils&version=1.8.0
#tool nuget:?package=Edi.AspNetCore.Utils&version=1.8.0
Edi.AspNetCore.Utils
Common ASP.NET Core helper classes for my own projects. Targets .NET 10.
Installation
dotnet add package Edi.AspNetCore.Utils
Features
ClientIPHelper
Extracts the real client IP address from an HttpContext, respecting common proxy and CDN forwarding headers.
Checked headers (in order of preference):
| Header | Used by |
|---|---|
X-Azure-ClientIP |
Azure Front Door |
CF-Connecting-IP |
Cloudflare |
X-Forwarded-For |
Standard proxy |
X-Real-IP |
Nginx |
X-Client-IP |
Apache |
True-Client-IP |
Akamai / Cloudflare Enterprise |
HTTP_X_FORWARDED_FOR |
IIS |
HTTP_CLIENT_IP |
Alternative |
Falls back to RemoteIpAddress when no public IP is found in headers. When a custom forwarded-for header is configured via UseSmartXFFHeader, the connection remote IP is returned directly.
string ip = ClientIPHelper.GetClientIP(httpContext);
SmartXFFHeaderExtensions
Middleware extension that configures ForwardedHeaders from appsettings.json. Automatically skips KnownProxies configuration when running in Docker.
app.UseSmartXFFHeader();
Configuration (appsettings.json):
{
"ForwardedHeaders": {
"HeaderName": "X-Forwarded-For",
"KnownProxies": [ "203.0.113.1", "203.0.113.2" ]
}
}
HeaderName— custom forwarded-for header name (optional, max 40 chars, must be a valid HTTP header name).KnownProxies— list of trusted proxy IP addresses (skipped in Docker environments).
VersionHelper
Reads version information from the entry assembly.
| Member | Description |
|---|---|
AppVersionBasic |
File version (AssemblyFileVersionAttribute), or "N/A". |
AppVersion |
Informational version with git hash shortened to 6 chars, e.g. 1.0.0 (ab12cd). |
IsNonStableVersion() |
true if AppVersion contains preview, beta, rc, debug, alpha, test, canary, or nightly. |
TryGetFullOSVersion() |
Full OS version string. On Windows, includes the UBR from the registry, e.g. Microsoft Windows 11 Pro 10.0.22631.4317. |
string version = VersionHelper.AppVersion;
bool isPreview = VersionHelper.IsNonStableVersion();
string os = VersionHelper.TryGetFullOSVersion();
EnvironmentHelper
Runtime environment detection and tag parsing.
bool onAzure = EnvironmentHelper.IsRunningOnAzureAppService(); // checks WEBSITE_SITE_NAME
bool inDocker = EnvironmentHelper.IsRunningInDocker(); // checks DOTNET_RUNNING_IN_CONTAINER
// Reads comma-separated tags from APP_TAGS (or a custom env var)
// Valid characters: a-z A-Z 0-9 - # @ $ ( ) [ ] /
IEnumerable<string> tags = EnvironmentHelper.GetEnvironmentTags();
IEnumerable<string> tags = EnvironmentHelper.GetEnvironmentTags("CUSTOM_TAGS");
SecurityHelper
URL sanitization and password hashing utilities.
SterilizeLink
Sanitizes a URL against open-redirect and SSRF attacks. Returns "#" for invalid, loopback, or private-IP URLs.
string safe = SecurityHelper.SterilizeLink(rawUrl);
IsPrivateIP
Tests whether an IPv4 address belongs to a private/LAN range (10.x, 172.16–31.x, 192.168.x, 127.x).
bool isPrivate = SecurityHelper.IsPrivateIP("192.168.1.1"); // true
HashPassword / GenerateSalt
PBKDF2 password hashing via Microsoft.AspNetCore.Cryptography.KeyDerivation.
string salt = SecurityHelper.GenerateSalt(); // 16 random bytes, Base64
string hash = SecurityHelper.HashPassword(password, salt);
Parameters for HashPassword: prf (default HMACSHA256), iterationCount (default 100000), numBytesRequested (default 32).
UrlExtension
Extension methods on string and Uri.
// Validate URL scheme
bool valid = url.IsValidUrl(); // HTTP or HTTPS
bool secure = url.IsValidUrl(UrlScheme.Https);
// Combine base URL with a path segment
string full = "https://example.com".CombineUrl("api/v1");
// → "https://example.com/api/v1"
// Detect localhost
bool local = new Uri("http://localhost:5000").IsLocalhostUrl();
DomainDataHelper
Typed wrapper around AppDomain.CurrentDomain data storage.
DomainDataHelper.SetAppDomainData("MyKey", myValue);
string value = DomainDataHelper.GetAppDomainData<string>("MyKey", defaultValue: "fallback");
ProblemDetailsResultFilter
MVC result filter that converts any non-ProblemDetails 4xx/5xx ObjectResult into RFC 7807/9457 Problem Details format, including a traceId extension.
builder.Services.AddControllers(options =>
{
options.Filters.Add<ProblemDetailsResultFilter>();
});
Controllers can continue returning semantic helpers and still produce consistent error responses:
return NotFound("Resource not found."); // → ProblemDetails with status 404
ProblemDetailsStatusCodePages
A StatusCodePages handler that writes RFC 7807 Problem Details for responses with no body (e.g. 404 from middleware).
app.UseStatusCodePages(ProblemDetailsStatusCodePages.Handler);
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Edi.AspNetCore.Utils:
| Repository | Stars |
|---|---|
|
EdiWang/Moonglade
Blog system of https://edi.wang, runs on Microsoft Azure
|