NetUtils.Core 1.0.3

dotnet add package NetUtils.Core --version 1.0.3
                    
NuGet\Install-Package NetUtils.Core -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NetUtils.Core" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NetUtils.Core" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="NetUtils.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NetUtils.Core --version 1.0.3
                    
#r "nuget: NetUtils.Core, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NetUtils.Core@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NetUtils.Core&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=NetUtils.Core&version=1.0.3
                    
Install as a Cake Tool

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

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 384 8/28/2023
1.0.2 356 8/19/2023
1.0.1 357 8/19/2023
1.0.0 361 8/19/2023