1

I am trying to use .NET's Core Identity, but via MongoDB instead SQL Server. I found a NuGet package that seems to fit the bill, but when I attempt to use it I get the following error:

System.MissingMethodException
  HResult=0x80131513
  Message=Method not found: 'Void MongoDB.Bson.BsonDefaults.set_GuidRepresentationMode(MongoDB.Bson.GuidRepresentationMode)'.
  Source=MongoDbGenericRepository
  StackTrace:
   at MongoDbGenericRepository.MongoDbContext.SetGuidRepresentation(GuidRepresentation guidRepresentation)
   at MongoDbGenericRepository.MongoDbContext.InitializeGuidRepresentation()
   at MongoDbGenericRepository.MongoDbContext..ctor(String connectionString, String databaseName)
   at Microsoft.Extensions.DependencyInjection.MongoDbIdentityBuilderExtensions.AddMongoDbStores[TUser,TRole,TKey](IdentityBuilder builder, String connectionString, String databaseName)
   at AspNetCore.Identity.MongoDbCore.Extensions.ServiceCollectionExtension.ConfigureMongoDbIdentity[TUser,TRole,TKey](IServiceCollection services, MongoDbIdentityConfiguration mongoDbIdentityConfiguration, IMongoDbContext mongoDbContext)

The NuGet package is:

enter image description here

I registered the GUID Representation Mode in the Program.cs as follows (I needed this for the other MongoDB operations requiring a GUID to be stored as the _id:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); // For MongoDB Guis

And in the later part of the Program.cs I have the following registration of the MongoDbIdentity service as follows:

var mongoDbSettings = builder.Configuration.GetSection(nameof(MongoDBSettings)).Get<MongoDBSettings>();

var mongoDbIdentityConfiguration = new MongoDbIdentityConfiguration
{
    MongoDbSettings = new MongoDbSettings
    {
        ConnectionString = mongoDbSettings?.ConnectionString,
        DatabaseName = mongoDbSettings?.DatabaseName,
        
    },
    IdentityOptionsAction = options =>
    {
        options.Password.RequireDigit = true;
        options.Password.RequiredLength = 8;
        options.Password.RequireNonAlphanumeric = true;
        options.Password.RequireUppercase = true;
        options.Password.RequireLowercase = true;

        // Lockout settings
        options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
        options.Lockout.MaxFailedAccessAttempts = 20;

        // ApplicationUser settings
        options.User.RequireUniqueEmail = false;
        options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.-_";
    }
};
builder.Services.ConfigureMongoDbIdentity<ApplicationUser, ApplicationRole, Guid>(mongoDbIdentityConfiguration);

The exception is triggered by this line:

builder.Services.ConfigureMongoDbIdentity<ApplicationUser, ApplicationRole, Guid>(mongoDbIdentityConfiguration);

I followed the following example:

https://www.yogihosting.com/aspnet-core-identity-mongodb/

My questions are:

  1. Is there something I need to do to help the package recognize the BSON GUID?
  2. The package seems fairly new (2024 last updated) but is anyone else using it?
  3. Is there a better way to do this? I wanted to be somewhat standard, but I could implement my own Identity service, I just wanted to be somewhat standard to use the Authorization/Authentication annotations.

2 Answers 2

2

The problem is that you are using AspNetCore.Identity.MongoDB while referencing MongoDB.Driver version 3+. The property that is accessed has been removed in driver version 3 hence the exception.

How to solve this:

  • Best, but slowest: try to get an updated version of the package, maybe by creating a PR or filing an issue in the project
  • Quick fix: downgrade your MongoDB driver to version 2.x which is not optimal, but should fix the issue.
  • I have seen that there are various projects that implement ASP.NET Core Identity with MongoDB, maybe there is an alternative package that works with driver version 3.x
Sign up to request clarification or add additional context in comments.

Comments

1

Ensure that all your model classes that use Guid properties (like Id, UserId, etc.) are annotated with [BsonRepresentation(BsonType.'Type')].

This tells the MongoDB driver to store the Guid as a string in the database and to correctly convert it when reading from or writing to MongoDB. MongoDB does not have a native data type for Guid.

[BsonRepresentation(BsonType.String)]
public Guid Id { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.