NetUtils.Core
1.0.3
dotnet add package NetUtils.Core --version 1.0.3
NuGet\Install-Package NetUtils.Core -Version 1.0.3
<PackageReference Include="NetUtils.Core" Version="1.0.3" />
<PackageVersion Include="NetUtils.Core" Version="1.0.3" />
<PackageReference Include="NetUtils.Core" />
paket add NetUtils.Core --version 1.0.3
#r "nuget: NetUtils.Core, 1.0.3"
#:package NetUtils.Core@1.0.3
#addin nuget:?package=NetUtils.Core&version=1.0.3
#tool nuget:?package=NetUtils.Core&version=1.0.3
NetUtils.Core Library
Welcome to NetUtils.Core, a versatile .NET NuGet library designed to simplify common tasks in your development projects. This library encompasses a collection of utilities, from handling datetime operations to enabling role-based authorization. Below, you'll find an overview of the key utilities included in this library along with examples of their usage.
Table of Contents
- Installation
- Work Context (HttpContextAccessor)
- JWT Token Helper
- Authorization Filters
- Datetime Broker
- Guid Broker
- Pagination
Installation
You can install the NetUtils.Core library via NuGet Package Manager:
Install-Package NetUtils.Core
Work Context (HttpContextAccessor)
The Work Context utility provides access to user roles, authorization tokens, and language preferences.
It's powered by the HttpContextAccessor and is essential for implementing role-based authorization and language handling.
Example Usage
public class MyService
{
private readonly IWorkContext _workContext;
public MyService(IWorkContext workContext)
{
_workContext = workContext;
}
public void PerformActionBasedOnRole()
{
string[] roles = _workContext.Roles;
string authToken = _workContext.AuthorizationToken;
string language = _workContext.Language;
// Implement role-based actions and language handling
}
}
JWT Token Helper
The JWT Token Helper utility assists in extracting claims from JWT tokens within the HttpContext.
It's crucial for authentication and authorization processes.
Example Usage
public class AuthenticationService
{
public void Authenticate(HttpContext httpContext)
{
string username = JwtTokenHelper.GetClaimValueFromToken<string>(httpContext, "username");
DateTime expiration = JwtTokenHelper.GetClaimValueFromToken<DateTime>(httpContext, "exp");
IEnumerable<Claim> = JwtTokenHelper.GetClaimsFromContext(httpContext);
string[] currentUserRoles = JwtTokenHelper.DeserializeValueFromType<string[]>(
claims,
claimType: "Roles");
// Perform authentication logic
}
}
Authorization Filters
The Authorization Filters utility simplifies role-based authorization within your API endpoints. It's achieved by applying AuthorizationAttribute to actions or controllers.
Authorization Types
The utility supports two types of authorization: AuthorizationType.All and AuthorizationType.Any.
AuthorizationType.All: Requires that the user must possess all the specified roles to access the action.AuthorizationType.Any: Allows the user to access the action if they have at least one of the specified roles.
Defining Authorized Roles
You can define authorized roles in the appsettings.json file under the AllowedRoles key:
"AuthorizationConfig": {
"AllowedRoles": [ "Admin", "Manager", "Employee" ]
}
or you can define allowed roles when using the attibute
[Authorization(AuthorizationType.All, "Admin", "Manager")]
[HttpGet("needAllRoles")]
public IActionResult NeedAllRoles()
{
// Action logic for authorized users
}
[Authorization(AuthorizationType.Any, "Admin", "Manager")]
[HttpGet("atLeastOneRole")]
public IActionResult AtLeastOneRole()
{
// Action logic for authorized users
}
Datetime Broker
The Datetime Broker utility centralizes datetime-related operations, ensuring consistency across time-related tasks. Can be used to ease the unit testing.
Example Usage
services.AddUtilityBrokers(); //call in services registration file
public class MyService
{
private readonly IDateTimeBroker dateTimeBroker;
public MyService(IDateTimeBroker dateTimeBroker)
{
this.dateTimeBroker = dateTimeBroker;
}
public void PerformActionWithTime()
{
DateTime currentUtcTime = this.dateTimeBroker.GetCurrentDateTime();
// Implement time-based actions
}
}
GUID Broker
The Guid Broker utility centralizes guid operations, ensuring consistency across guid generation-related tasks. Can be used to ease the unit testing.
Example Usage
services.AddUtilityBrokers(); //call in services registration file
public class MyService
{
private readonly IGuidBroker guidBroker;
public MyService(IGuidBroker guidBroker)
{
this.guidBroker = guidBroker;
}
public void PerformActionWithGuid()
{
Guid newGuid = this.guidBroker.GenerateGuid();
// Implement guid-based actions
}
}
Pagination
The Pagination utility simplifies retrieving and displaying paginated data.
Example Usage
Suppose you have a collection of data and you want to implement pagination for it. The Pagination utility provides a convenient way to achieve this. Let's look at some examples of how to use it:
Basic Pagination
var pagination = new Pagination<MyModel, int>
{
OrderBy = model => model.Id,
Page = 1,
PageSize = 10,
OrderByDescending = true
};
IQueryable<MyModel> data = _dataService.GetData();
var pagedData = data.PageBy(pagination);
// pagedData now contains the first 10 items ordered by Id in descending order
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.IdentityModel.JsonWebTokens (>= 6.30.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.