N2.Core.Identity 1.1.0

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

N2.Core.Identity

Description

N2.Core.Identity is a flexible identity and role management library for .NET applications, built on top of ASP.NET Core Identity. It provides a strongly-typed, extensible N2IdentityDbContext (extending IdentityDbContext) and a feature-rich N2UserManager for advanced user, role, and authentication scenarios. The library is designed for modern .NET (net8.0, net9.0) and supports custom user properties, role management, and token-based workflows.

Features

  • Extends IdentityDbContext for easy integration with ASP.NET Core Identity.
  • Strongly-typed ApplicationUser and ApplicationRole with support for custom properties.
  • Advanced user and role management via N2UserManager.
  • Token generation and email confirmation workflows.
  • Designed for .NET 8 and .NET 9.

License

AFL-3.0

Basic Setup

  1. Configure the DbContext in your application: NuGet
  2. Register the N2UserManager:
  3. Configure Identity (optional, for ASP.NET Core):
  4. Use N2UserManager in your application:
using N2.Core.Identity.Services; using N2.Core.Identity.Data;
public class AccountService { private readonly IUserManager<ApplicationUser> _userManager;
   public AccountService(IUserManager<ApplicationUser> userManager)
   {
       _userManager = userManager;
   }

   public async Task CreateUserAsync(string email, string password)
   {
       var user = new ApplicationUser { UserName = email, Email = email };
       await _userManager.CreateAsync(user, password, CancellationToken.None);
   }
}

Best Practices

  1. Secure Configuration
  • Connection Strings: Store database connection strings securely (e.g., environment variables, Azure Key Vault, or user secrets in development).
  • Sensitive Data: Never log sensitive information such as passwords or tokens.
  1. Dependency Injection
  • Always register N2UserManager and related services (N2IdentityContext, ApplicationUser, ApplicationRole) using dependency injection. This ensures proper lifetime management and testability.
  1. Error Handling
  • Always check the result of N2UserManager methods. Most methods return an ICommandResponse or similar result object—verify Status.IsSuccess() before proceeding.
  • Handle and log errors gracefully, but avoid exposing internal details to end users.
  1. User Input Validation
  • Validate all user input before passing it to N2UserManager methods. This includes usernames, emails, and passwords.
  • Use strong password policies and validate emails for format and uniqueness.
  1. Token Management
  • Use the provided token generation and validation methods for email confirmation and password reset. Tokens should be time-limited and single-use where possible.
  • Never expose raw tokens in logs or error messages.
  1. Role Management
  • Use AddToRoleAsync, RemoveFromRoleAsync, and RoleExistsAsync to manage user roles. Always check for role existence before assignment.
  • Avoid hardcoding role names; use constants or configuration.
  1. DbContext Lifetime
  • Ensure that the N2IdentityContext (or your DbContext) is registered with a scoped lifetime (the default for EF Core in ASP.NET Core).
  • Avoid sharing DbContext instances across threads.
  1. Concurrency and Transactions
  • Be aware of concurrency issues, especially when updating user or role data. Use transactions if multiple changes must be atomic.
  • Handle potential DbUpdateConcurrencyException or similar exceptions.
  1. Security Practices
  • Always hash and salt passwords using the built-in mechanisms.
  • Use HTTPS for all communications.
  • Enable account lockout and email confirmation for new users.
  1. Testing
  • Write unit and integration tests for all user management workflows.
  • Use test doubles or in-memory databases for testing, not production data.
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 is compatible.  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.1.0 162 6/23/2025
1.0.10 129 8/5/2024
1.0.9 97 8/5/2024
1.0.6 109 8/1/2024
1.0.5 136 5/13/2024
1.0.4 139 5/13/2024
1.0.3 138 5/8/2024