Identity.Mongo 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Identity.Mongo --version 1.0.1
                    
NuGet\Install-Package Identity.Mongo -Version 1.0.1
                    
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="Identity.Mongo" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Identity.Mongo" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Identity.Mongo" />
                    
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 Identity.Mongo --version 1.0.1
                    
#r "nuget: Identity.Mongo, 1.0.1"
                    
#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 Identity.Mongo@1.0.1
                    
#: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=Identity.Mongo&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Identity.Mongo&version=1.0.1
                    
Install as a Cake Tool

Identity.Mongo

NuGet

Overview

Identity.Mongo is an open-source library that extends Identity Framework to integrate MongoDB as the backing store. It enables user authentication, roles, and claims management using MongoDB.

Installation

Install the package via NuGet:

dotnet add package Identity.Mongo

Or through the NuGet Package Manager:

Install-Package Identity.Mongo

Getting Started

Step 1: Create a Class that Extends the Base Class

To use the library, create a new class that extends the provided base class MongoDbContext:

using Identity.Mongo;

public class AppDbContext : MongoDbContext
{
    protected override void ConfigureCollections()
    {
        // Add your collections here
    }
}

Step 2: Register the Class in Program.cs

In your Program.cs, register the extended class as a service. For example:

using Identity.Mongo;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddMongoDb<AppDbContext>("connectionString","databaseName");

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

var app = builder.Build();

// Configure the HTTP request pipeline.
// Additional middleware setup if necessary

app.Run();

Usage

Identity Framework: Now that you configured your db context and added mongo stores to identity framework you could start using identity framework as normal

public class MyController(UserManager<IdentityUser> uMgr) : ControllerBase
{
    [HttpGet("/add-user")]
    public async Task<IActionResult> AddAndGetUser()
    {
        var result = await uMgr.CreateAsync(new IdentityUser
        {
            Email = "test@test.com",
            UserName = "abdo"
        });
        
        return Ok(result);
    }
}

Optionally you could add collections to your db context by using the built in Database property.

using Identity.Mongo;

public class AppDbContext : MongoDbContext
{
    protected override void ConfigureCollections()
    {
        Collection = Database.GetCollection<YourModel>("collectionName");
    }

    public IMongoCollection<YourModel> Collection { get; set; }
}

Then inject it in other classes and start using the context to access mongodb.

public class MyController(AppDbContext ctx) : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> DoSomething()
    {
        var results = ctx.Collection
                        .Find(_ => true)
                        .ToListAsync();
        
        return Ok(results);
    }
}

Support

If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.

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 was computed.  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.

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
1.1.0 139 10/21/2024
1.0.2 104 10/16/2024
1.0.1 105 10/16/2024
1.0.0 112 10/16/2024
1.0.0-alpha.6 68 10/15/2024
1.0.0-alpha.5 69 10/14/2024
1.0.0-alpha.3 69 10/13/2024
1.0.0-alpha.2 65 10/13/2024
1.0.0-alpha.1 65 10/13/2024