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" />
<PackageReference Include="N2.Core.Identity" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=N2.Core.Identity&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
andApplicationRole
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
- Configure the DbContext in your application:
- Register the N2UserManager:
- Configure Identity (optional, for ASP.NET Core):
- 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
- 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.
- Dependency Injection
- Always register N2UserManager and related services (N2IdentityContext, ApplicationUser, ApplicationRole) using dependency injection. This ensures proper lifetime management and testability.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Testing
- Write unit and integration tests for all user management workflows.
- Use test doubles or in-memory databases for testing, not production data.
Product | Versions 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.
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.17)
- Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore (>= 8.0.17)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 8.0.17)
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.6)
- Microsoft.Extensions.Configuration.UserSecrets (>= 9.0.6)
- Microsoft.IdentityModel.Tokens (>= 8.12.1)
- n2.core.abstractions (>= 1.4.0)
- System.IdentityModel.Tokens.Jwt (>= 8.12.1)
-
net9.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.6)
- Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore (>= 9.0.6)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 9.0.6)
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.6)
- Microsoft.Extensions.Configuration.UserSecrets (>= 9.0.6)
- Microsoft.IdentityModel.Tokens (>= 8.12.1)
- n2.core.abstractions (>= 1.4.0)
- System.IdentityModel.Tokens.Jwt (>= 8.12.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.