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
<PackageReference Include="Rammi.MongoDB.AspNetCore.Identity" Version="2.0.2" />
<PackageVersion Include="Rammi.MongoDB.AspNetCore.Identity" Version="2.0.2" />
<PackageReference Include="Rammi.MongoDB.AspNetCore.Identity" />
paket add Rammi.MongoDB.AspNetCore.Identity --version 2.0.2
#r "nuget: Rammi.MongoDB.AspNetCore.Identity, 2.0.2"
#:package Rammi.MongoDB.AspNetCore.Identity@2.0.2
#addin nuget:?package=Rammi.MongoDB.AspNetCore.Identity&version=2.0.2
#tool nuget:?package=Rammi.MongoDB.AspNetCore.Identity&version=2.0.2
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:
- Package name changed:
MongoDB.AspNet.Identity→MongoDB.AspNetCore.Identity - Namespace changed:
MongoDB.AspNet.Identity→MongoDB.AspNetCore.Identity - Target framework: .NET Framework 4.5.2 → .NET 10
- Identity system:
Microsoft.AspNet.Identity→Microsoft.AspNetCore.Identity - Proper async/await: All database operations now properly await MongoDB operations
Migration Steps:
- Update to .NET 10
- Replace package references
- Update namespace imports
- Update service registration (see Quick Start above)
- 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
- Indexes: The library automatically creates indexes on
NormalizedUserName(unique) andNormalizedEmail - Connection pooling: Reuse
MongoClientinstances (registered as Singleton) - Async all the way: All operations are properly async - don't use
.Resultor.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
- Original project by jsheely and sitebro
- Updated to .NET 10 and modern standards by the community
- Special thanks to David Boike whose RavenDB AspNet Identity project provided the initial foundation
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 | 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 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. |
-
.NETFramework 4.8
- Microsoft.Extensions.Identity.Stores (>= 8.0.0)
- MongoDB.Driver (>= 3.5.0)
-
net10.0
- MongoDB.Driver (>= 3.5.0)
-
net8.0
- MongoDB.Driver (>= 3.5.0)
-
net9.0
- MongoDB.Driver (>= 3.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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/