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:
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:
- Is there something I need to do to help the package recognize the BSON GUID?
- The package seems fairly new (2024 last updated) but is anyone else using it?
- 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.
