huzcodes.Extensions 1.3.0

dotnet add package huzcodes.Extensions --version 1.3.0                
NuGet\Install-Package huzcodes.Extensions -Version 1.3.0                
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="huzcodes.Extensions" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add huzcodes.Extensions --version 1.3.0                
#r "nuget: huzcodes.Extensions, 1.3.0"                
#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.
// Install huzcodes.Extensions as a Cake Addin
#addin nuget:?package=huzcodes.Extensions&version=1.3.0

// Install huzcodes.Extensions as a Cake Tool
#tool nuget:?package=huzcodes.Extensions&version=1.3.0                

huzcodes.Extensions

huzcodes.Extensions is a C# .NET 8 package that provides a global exception handler to enhance error handling in your applications. It offers two main classes, 'ResultException' and 'CustomResultException', for managing Extensions and errors in a unified manner. 'ResultException' allows you to throw Extensions with a specified error message and status code, while 'CustomResultException' is designed for structured responses in case of failures.

Additionally, the package includes 'fluent validation support', enabling you to handle validation errors seamlessly. By centralizing error management, huzcodes.Extensions simplifies error handling across different layers of your application.

huzcodes.Extensions now includes an identity extension for token generation and authorization using JWT tokens or access keys and it supports authorization for both together as well at the same time. This allows client applications, including mobile or websites, to access APIs using JWT tokens. Additionally, APIs can access other APIs using an access key.

Installation

To install huzcodes.Extensions, use the following command in the Package Manager Console:


dotnet add package huzcodes.Extensions --version 1.0.0

Exception and Validation Extension


Usage

To use huzcodes.Extensions, register the exception handler extension and, if needed, fluent validation in your application's program file:


// Register the exception handler extension
app.AddExceptionHandlerExtension();

// Register fluent validation (if needed)
builder.Services.AddFluentValidation(typeof(Program));

Example

Here's an example of how you can use 'ResultException':


throw new ResultException("response error message", (int)HttpStatusCode.BadRequest);

And here's an example of how you can use 'CustomResultException':

throw new CustomResultException(new CustomExceptionResponse()
{
    Message = "response error message",
    StatusCode = (int)HttpStatusCode.BadRequest,
    ClassName = nameof(ExtensionsController),
    FunctionName = nameof(Get),
});

Identity Extension


Registering the Identity Extension

To use the identity extension, register the necessary services in your application's program file:

// Add huzcodes identity extension registration
var oSigningKey = builder.Configuration["SigningKey"];
builder.Services.AddAuthZ(oSigningKey!);

// Add the huzcodes identity extension middleware
app.AddAuthZMiddleWare();

Add the '[Authorize]' attribute on top of controllers or specific action methods/endpoints to enable authorization.

Usage

Generating a Token

Inject the 'IIdentityManager' interface and use the 'GenerateJwtToken' method to generate a token:

var oIdentity = new IdentityModel()
{
    Email = "huz@huzcodes.com",
    FirstName = "huz",
    LastName = "codes"
};
var oSigningKey = _configuration["SigningKey"];
var oToken = _identityManager.GenerateJwtToken(oIdentity,
                                               oSigningKey!,
                                               DateTime.UtcNow.AddHours(1));

Decoding a Token

By Claims Generic Class, you need to pass same generic class type that used while generating the token and with same prapeters.

var oTokenContent = _identityManager.DecodeToken<IdentityModel>();

By Passing the Token to Function Parameters, First, use the 'GetAuthorizationHeader' extension method "it is part of the package", for 'IHttpContextAccessor' to get the token from the header:

var oJwtToken = _httpContextAccessor.GetAuthorizationHeader();
var oTokenContent = _identityManager.DecodeToken<IdentityModel>(jwtToken: oJwtToken);

Access Key Authorization

To use access key authorization, add the access key in the app settings JSON file as "X-Api-Key" and the key name in app settings JSON must by like this name "X-Api-Key" for the middleware authorization by api key to work correct.

For more information on how to use huzcodes.Extensions, please refer to the API Package Tests.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.3.0 270 6/30/2024
1.2.0 261 2/20/2024
1.1.0 131 2/20/2024
1.0.0 108 2/18/2024

huzcodes.Extensions now includes an identity extension for token generation and authorization using JWT tokens or access keys and it supports authorization for both together as well at the same time. This allows client applications, including mobile or websites, to access APIs using JWT tokens. Additionally, APIs can access other APIs using an access key.