MadEyeMatt.AspNetCore.Identity.MongoDB 7.2.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package MadEyeMatt.AspNetCore.Identity.MongoDB --version 7.2.3                
NuGet\Install-Package MadEyeMatt.AspNetCore.Identity.MongoDB -Version 7.2.3                
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="MadEyeMatt.AspNetCore.Identity.MongoDB" Version="7.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MadEyeMatt.AspNetCore.Identity.MongoDB --version 7.2.3                
#r "nuget: MadEyeMatt.AspNetCore.Identity.MongoDB, 7.2.3"                
#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.
// Install MadEyeMatt.AspNetCore.Identity.MongoDB as a Cake Addin
#addin nuget:?package=MadEyeMatt.AspNetCore.Identity.MongoDB&version=7.2.3

// Install MadEyeMatt.AspNetCore.Identity.MongoDB as a Cake Tool
#tool nuget:?package=MadEyeMatt.AspNetCore.Identity.MongoDB&version=7.2.3                

AspNetCore.Identity.MongoDB

A libary that provides MongoDB UserStore and RoleStore implementations for ASP.NET Identity Core. It allows you to use MongoDB with ASP.NET Core Identity.

Identity Entities

If you choose to use custom user and role entities be reminded that those entities must inherit from MongoIdentityUser<TKey> and MongoIdentityRole<TKey>, where TKey is the type of the ID of the entity. The library supports either string or Guid type IDs. When string is used, the MongoDB native ObjectId will be used as database ID type.

The following example shows custom user and role entities:

public class ApplicationUser : MongoIdentityUser<string>
{
	public ApplicationUser()
	{
	}

	public ApplicationUser(string userName) 
		: base(userName)
	{
	}

	public string FirstName { get; set; }

	public string LastName { get; set; }
}

public class ApplicationRole : MongoIdentityRole<string>
{
	public ApplicationRole()
	{
	}

	public ApplicationRole(string roleName)
		: base(roleName)
	{
	}

	public string DisplayText { get; set; }
}

Using custom entity classes allows the developer to extend to store additional data besides the data needed by the Identity system.

If no custom enities are needed, one can use the default implementations using string IDs:

  • MongoIdentityUser
  • MongoIdentityRole

Usage

To use the MongoDB stores with ASP.NET Identity use the IdentityBuilder extension AddMongoDbStores and configure the MongoDbContext using the AddMongoDbContext extension.

The stores support user/role and user-only configuration.

Configure user/role mode

builder.Services
	.AddAuthentication(IdentityConstants.ApplicationScheme)
	.AddIdentityCookies();

builder.Services.AddMongoDbContext<MongoDbContext>(options =>
{
	options.ConnectionString = "mongodb://localhost:27017";
	options.DatabaseName = "identity";
})
.AddIdentityCore<MongoIdentityUser>()
.AddRoles<MongoIdentityRole>()
.AddDefaultTokenProviders()
.AddMongoDbStores<SampleContext>();

Configure user-only mode

builder.Services
	.AddAuthentication(IdentityConstants.ApplicationScheme)
	.AddIdentityCookies();

builder.Services.AddMongoDbContext<MongoDbContext>(options =>
{
	options.ConnectionString = "mongodb://localhost:27017";
	options.DatabaseName = "identity";
})
.AddIdentityCore<MongoIdentityUser>()
.AddDefaultTokenProviders()
.AddMongoDbStores<SampleContext>();

Using a custom context

To use a custom MongoDbContext just create a new class that inherits from MongoDbContext. Using this context one can change the used collection names. The default names are AspNetUsers and AspNetRoles. The sample application shows how to change them into custom names.

Initialize the MongoDB driver

After configuring the service of the system one has to initialize the MongoDB stores database using the IServiceProvider extension InitializeMongoDbStores. The configuration will ensure the needed conventions are setup for the MongoDB driver and will ensure that the collections are created with the needed indexes for the user and role entities.

await app.Services.InitializeMongoDbStores();

Data Protection support

If the Identity system was configured to opt-in to use the data protection API the MongoDB driver will be automatically configured to protect/unprotect properties annotated with the [ProtectedPersonalData] attribute by adding a specialized string serializer. You will still need to implement the needed services and specialized user store that implements IProtectedUserStore<TUser>.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MadEyeMatt.AspNetCore.Identity.MongoDB:

Package Downloads
MadEyeMatt.AspNetCore.Identity.Permissions.MongoDB

A libary that adds permission-based authorization.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.3 31 12/17/2024
9.0.2 308 11/16/2024
9.0.1 105 11/14/2024
9.0.0 82 11/14/2024
8.0.8 117 11/1/2024
8.0.7 1,417 7/9/2024
8.0.6 151 6/2/2024
8.0.5 176 5/23/2024
8.0.4 177 4/18/2024
8.0.3 203 3/19/2024
8.0.2 301 1/4/2024
8.0.1 266 11/24/2023
8.0.0 191 11/16/2023
7.2.5 142 11/12/2023
7.2.4 242 7/20/2023
7.2.3 253 4/25/2023
7.2.2 208 4/13/2023
7.2.1 217 4/13/2023
7.2.0 198 4/12/2023
7.1.6 196 4/9/2023
7.1.5 212 4/9/2023
7.1.4 311 3/31/2023
7.1.2 304 3/28/2023
7.1.1 201 3/28/2023
7.1.0 182 3/28/2023
7.0.1 209 3/28/2023
7.0.0 195 3/28/2023