Edi.AspNetCore.Utils
2.0.0
dotnet add package Edi.AspNetCore.Utils --version 2.0.0
NuGet\Install-Package Edi.AspNetCore.Utils -Version 2.0.0
<PackageReference Include="Edi.AspNetCore.Utils" Version="2.0.0" />
<PackageVersion Include="Edi.AspNetCore.Utils" Version="2.0.0" />
<PackageReference Include="Edi.AspNetCore.Utils" />
paket add Edi.AspNetCore.Utils --version 2.0.0
#r "nuget: Edi.AspNetCore.Utils, 2.0.0"
#:package Edi.AspNetCore.Utils@2.0.0
#addin nuget:?package=Edi.AspNetCore.Utils&version=2.0.0
#tool nuget:?package=Edi.AspNetCore.Utils&version=2.0.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
Returns the client IP address from HttpContext.Connection.RemoteIpAddress. Configure ASP.NET Core Forwarded Headers Middleware before calling this helper when the application runs behind a proxy. Raw forwarding headers are not read directly because clients can spoof them.
string ip = ClientIPHelper.GetClientIP(httpContext);
IPAddressHelper
Classifies ordinary public Internet IPv4 and IPv6 addresses. Private, loopback, link-local, documentation, reserved, multicast, transition, tunneling, and other special-use addresses return false. IPv4-mapped IPv6 addresses are normalized before classification.
bool publicIPv4 = IPAddressHelper.IsPublicIPAddress("8.8.8.8"); // true
bool privateIPv4 = IPAddressHelper.IsPublicIPAddress("192.168.1.1"); // false
bool loopbackIPv6 = IPAddressHelper.IsPublicIPAddress("::1"); // false
Public HTTP client safety
AddPublicHttpClientSafety registers reusable DNS validation and HTTP connection services for applications that must fetch user-controlled public URLs.
builder.Services.AddPublicHttpClientSafety();
builder.Services.AddHttpClient<MyPublicUrlClient>()
.ConfigurePrimaryHttpMessageHandler(serviceProvider =>
serviceProvider.GetRequiredService<PublicHttpMessageHandlerFactory>().Create());
Use IPublicHttpUrlValidator.IsPublicHttpUrlAsync before business processing. The handler performs the authoritative check immediately before connecting and binds the socket to a checked address. It rejects hosts with any non-public DNS result and disables redirects, cookies, and proxy use. Applications must validate every redirect location before issuing the next request.
SmartXFFHeaderExtensions
Middleware extension that configures ForwardedHeaders from appsettings.json.
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. When empty or entirely invalid, ASP.NET Core's default loopback-only trusted proxies are retained. A valid explicit list replaces those defaults. Trust behavior does not change automatically in containers.
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 links by allowing local absolute paths and HTTP/HTTPS links. Literal IP hosts must be ordinary public Internet addresses. This method does not resolve DNS names and is not a replacement for the public HTTP client safety services when the server makes an outbound request.
string safe = SecurityHelper.SterilizeLink(rawUrl);
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
|