MadEyeMatt.AspNetCore.Identity.MongoDB 8.0.5

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

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

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 net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  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 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. 
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.2 79 11/16/2024
9.0.1 86 11/14/2024
9.0.0 69 11/14/2024
8.0.8 105 11/1/2024
8.0.7 1,105 7/9/2024
8.0.6 143 6/2/2024
8.0.5 168 5/23/2024
8.0.4 170 4/18/2024
8.0.3 199 3/19/2024
8.0.2 297 1/4/2024
8.0.1 261 11/24/2023
8.0.0 186 11/16/2023
7.2.5 138 11/12/2023
7.2.4 238 7/20/2023
7.2.3 249 4/25/2023
7.2.2 204 4/13/2023
7.2.1 212 4/13/2023
7.2.0 194 4/12/2023
7.1.6 192 4/9/2023
7.1.5 208 4/9/2023
7.1.4 306 3/31/2023
7.1.2 300 3/28/2023
7.1.1 197 3/28/2023
7.1.0 178 3/28/2023
7.0.1 205 3/28/2023
7.0.0 191 3/28/2023