Ethik.Utility.Security 1.0.2

dotnet add package Ethik.Utility.Security --version 1.0.2
                    
NuGet\Install-Package Ethik.Utility.Security -Version 1.0.2
                    
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="Ethik.Utility.Security" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ethik.Utility.Security" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Ethik.Utility.Security" />
                    
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 Ethik.Utility.Security --version 1.0.2
                    
#r "nuget: Ethik.Utility.Security, 1.0.2"
                    
#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 Ethik.Utility.Security@1.0.2
                    
#: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=Ethik.Utility.Security&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Ethik.Utility.Security&version=1.0.2
                    
Install as a Cake Tool

Ethik.Utility.Security NuGet Package (not updated doc)

Overview

The Ethik.Utility.Security NuGet package provides a set of utilities for handling security tasks in .NET applications. It includes tools to deal with passwords, token managements, and required extensions.

Features

  • Password: Tools to hash password and verify hashed passwords.
  • JWT Service: Tool to Generate jwt token and extract token details.
    • Service Collection Extensions: Extensins provided to add jwt in pipeline
      • AddJwtHelper: Adds JWT token services and settings.

Installation

You can install the package via NuGet Package Manager or by running the following command in the Package Manager Console:

Install-Package Ethik.Utility.Security

Or, add the package to your .csproj file:

<PackageReference Include="Ethik.Utility.Security" Version="1.0.0" />

Components

JwtTokenService Class

Provides functionality for generating and validating JWT tokens for authentication purposes.

Properties
  • _jwtSettings: Stores the JWT settings (JwtSettings) used to configure the token generation process. This includes values like the secret key, issuer, audience, and token expiry time.
Methods
  • GenerateToken(string userId, string email, string role):

    • Description: Generates a JWT token based on the provided user details, including user ID, email, and role.
    • Parameters:
      • userId: The unique identifier of the user.
      • email: The user's email address.
      • role: The user's role (e.g., Admin, User).
    • Returns: A string representation of the generated JWT token.
    • Example Usage:
      var jwtService = new JwtTokenService(options);
      var token = jwtService.GenerateToken("123", "user@example.com", "Admin");
      Console.WriteLine(token);
      
  • GetTokenDetails(string token):

    • Description: Extracts details from the provided JWT token, including expiration time and token type.
    • Parameters:
      • token: The JWT token to extract details from.
    • Returns: A JwtTokenDetails object containing the expiration time and token type.
    • Example Usage:
      var jwtService = new JwtTokenService(options);
      var tokenDetails = jwtService.GetTokenDetails(token);
      Console.WriteLine($"Token expires at: {tokenDetails.Expiration}");
      

JwtTokenDetails Class

Represents details about a JWT token, including expiration time and token type.

Properties
  • Token: The JWT token string.
  • Expiration: The expiration time of the token.
  • TokenType: The type of the token (e.g., "Bearer").

JwtSettings Class

Stores configuration settings required for generating JWT tokens.

Properties
  • Issuer: The issuer of the JWT token.
  • Audience: The audience for the JWT token.
  • SecretKey: The secret key used to sign the JWT token.
  • ExpiryMinutes: The number of minutes until the token expires.

ServiceCollectionExtensions Class

Provides extension methods for configuring JWT authentication and adding the JwtTokenService to the service collection.

Methods
  • AddJwtHelper(IServiceCollection services, IConfiguration configuration):
    • Description: Configures JWT authentication and registers the JwtTokenService in the service collection.
    • Parameters:
      • services: The IServiceCollection to add the JWT authentication to.
      • configuration: The application's configuration section containing JWT settings.
    • Returns: The updated IServiceCollection.
    • Example Usage:
      public class Startup
      {
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddJwtHelper(Configuration);
          }
      }
      
Example Usage
// In Program.cs or Startup.cs
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Add JWT authentication and helper service.
        services.AddJwtHelper(Configuration);
    }

    // Other methods...
}

JwtSettings Configuration Example

Here's an example configuration in appsettings.json:

{
  "JwtSettings": {
    "SecretKey": "super_secret_key_12345",
    "Issuer": "MyApp",
    "Audience": "MyAppAudience",
    "ExpiryMinutes": 60
  }
}

In your Program.cs or Startup.cs, you would load the configuration and set up JWT authentication as shown in the examples above.

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.  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.2 125 4/26/2025
1.0.1 153 10/11/2024
1.0.0 148 9/30/2024