Rammi.MongoDB.AspNetCore.Identity 2.0.2

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

MongoDB.AspNetCore.Identity

ASP.NET Core Identity provider that uses MongoDB for storage

Overview

This library provides a MongoDB-based implementation of ASP.NET Core Identity, offering a modern alternative to Entity Framework-based storage. Built for .NET 10 with the latest MongoDB driver (3.1.0), it provides full async/await support and proper error handling.

Features

  • Modern .NET 10 support
  • Latest MongoDB Driver 3.1.0 with full async/await
  • Single MongoDB collection (AspNetUsers) - simpler than EF's multi-table approach
  • Full ASP.NET Core Identity integration
  • Nullable reference types support
  • ✅ Implements all standard Identity interfaces:
    • IUserStore<TUser>
    • IUserLoginStore<TUser>
    • IUserRoleStore<TUser>
    • IUserClaimStore<TUser>
    • IUserPasswordStore<TUser>
    • IUserSecurityStampStore<TUser>
    • IUserEmailStore<TUser>
    • IUserPhoneNumberStore<TUser>
    • IUserLockoutStore<TUser>
    • IUserTwoFactorStore<TUser>

Installation

dotnet add package MongoDB.AspNetCore.Identity

Or via Package Manager Console:

Install-Package MongoDB.AspNetCore.Identity

Quick Start

1. Configure Services

In your Program.cs:

using MongoDB.AspNetCore.Identity;
using MongoDB.Driver;

var builder = WebApplication.CreateBuilder(args);

// Configure MongoDB
var mongoClient = new MongoClient("mongodb://localhost:27017");
var mongoDatabase = mongoClient.GetDatabase("YourDatabaseName");

// Add Identity with MongoDB
builder.Services.AddSingleton<IMongoDatabase>(mongoDatabase);
builder.Services.AddScoped<UserStore<IdentityUser>>();

builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddUserStore<UserStore<IdentityUser>>()
    .AddDefaultTokenProviders();

var app = builder.Build();

2. Using Connection String from Configuration

In appsettings.json:

{
  "MongoDB": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "YourDatabaseName"
  }
}

In Program.cs:

using MongoDB.AspNetCore.Identity;
using MongoDB.Driver;

var builder = WebApplication.CreateBuilder(args);

var mongoConnectionString = builder.Configuration["MongoDB:ConnectionString"]!;
var mongoDatabaseName = builder.Configuration["MongoDB:DatabaseName"]!;

var mongoClient = new MongoClient(mongoConnectionString);
var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName);

builder.Services.AddSingleton<IMongoDatabase>(mongoDatabase);
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddUserStore<UserStore<IdentityUser>>()
    .AddDefaultTokenProviders();

3. Custom User Model

Create your own user class:

using MongoDB.AspNetCore.Identity;

public class ApplicationUser : IdentityUser
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

Then use it in your configuration:

builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddUserStore<UserStore<ApplicationUser>>()
    .AddDefaultTokenProviders();

Usage Examples

Register a User

public class AccountController : Controller
{
    private readonly UserManager<IdentityUser> _userManager;
    private readonly SignInManager<IdentityUser> _signInManager;

    public AccountController(
        UserManager<IdentityUser> userManager,
        SignInManager<IdentityUser> signInManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
    }

    [HttpPost]
    public async Task<IActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new IdentityUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            
            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }
            
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }
        return View(model);
    }
}

Login

[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        var result = await _signInManager.PasswordSignInAsync(
            model.Email, 
            model.Password, 
            model.RememberMe, 
            lockoutOnFailure: false);
            
        if (result.Succeeded)
        {
            return RedirectToAction("Index", "Home");
        }
        
        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
    }
    return View(model);
}

Migration from Old Version

If you're migrating from the old MongoDB.AspNet.Identity package (for .NET Framework):

Key Changes:

  1. Package name changed: MongoDB.AspNet.IdentityMongoDB.AspNetCore.Identity
  2. Namespace changed: MongoDB.AspNet.IdentityMongoDB.AspNetCore.Identity
  3. Target framework: .NET Framework 4.5.2 → .NET 10
  4. Identity system: Microsoft.AspNet.IdentityMicrosoft.AspNetCore.Identity
  5. Proper async/await: All database operations now properly await MongoDB operations

Migration Steps:

  1. Update to .NET 10
  2. Replace package references
  3. Update namespace imports
  4. Update service registration (see Quick Start above)
  5. The MongoDB collection structure remains compatible

Connection String Formats

Standard MongoDB Connection String

mongodb://localhost:27017

With Authentication

mongodb://username:password@localhost:27017

MongoDB Atlas

mongodb+srv://username:password@cluster.mongodb.net/

Requirements

  • .NET 10 or later
  • MongoDB Server 4.0 or later (5.0+ recommended)
  • MongoDB.Driver 3.1.0 or later

Performance Tips

  1. Indexes: The library automatically creates indexes on NormalizedUserName (unique) and NormalizedEmail
  2. Connection pooling: Reuse MongoClient instances (registered as Singleton)
  3. Async all the way: All operations are properly async - don't use .Result or .Wait()

Troubleshooting

Duplicate Key Error on User Creation

This means the username already exists. The library catches this and returns a proper IdentityResult.Failed with a "DuplicateUserName" error code.

User Not Found

Make sure your MongoDB connection string and database name are correct. Check that the AspNetUsers collection exists in your database.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - See LICENSE file for details

Credits

Support

For issues and questions:

  • Open an issue on GitHub
  • Check existing issues for solutions

Version History

2.0.0 (2025)

  • Migrated to .NET 10
  • Updated to MongoDB.Driver 3.1.0
  • Migrated from Microsoft.AspNet.Identity to Microsoft.AspNetCore.Identity
  • Fixed all async/await issues for proper asynchronous operations
  • Modern SDK-style project format
  • Full nullable reference types support
  • Proper error handling and validation
  • Breaking changes: See Migration section

1.0.7 (2018)

  • Last version for .NET Framework
  • Added email, lockout, and two-factor authentication support
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 is compatible.  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. 
.NET Framework net48 is compatible.  net481 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
2.0.2 204 11/23/2025
2.0.1 418 11/17/2025
2.0.0 415 11/17/2025

Version 2.0.2
     - Updated to target .NET 10
     
     Version 2.0.1
     - Updated namespace to Rammi.MongoDB.AspNetCore.Identity
     - Fixed GitHub repository URLs
     
     Version 2.0.0
     - Migrated to .NET 10
     - Updated to MongoDB.Driver 3.1.0
     - Migrated from Microsoft.AspNet.Identity to Microsoft.AspNetCore.Identity
     - Fixed all async/await issues for proper asynchronous operations
     - Modern SDK-style project format
     - Full nullable reference types support
     
     Maintained by Rammi - https://rammi.cz/