I'm currently trying to learn ASP.NET Core 3.1 in combination with MongoDB. I can already perform simple CRUD operations. Now I want to set up ASP.NET Core Identity in connection with MongoDB. For this I have installed the following Nuget packages:
- AspNetCore.Identity.Mongo (version 8.1.0)
- AspNetCore.Identity.MongoDbCore (version 3.1.1)
In the IdentityHostingStartup class in the Configure method, I am now executing the following code:
builder.ConfigureServices((context, services) => {
services.AddIdentityMongoDbProvider<ApplicationUser, MongoRole>(identityOptions =>
{
// Password settings.
identityOptions.Password.RequiredLength = 6;
identityOptions.Password.RequireLowercase = true;
identityOptions.Password.RequireUppercase = true;
identityOptions.Password.RequireNonAlphanumeric = false;
identityOptions.Password.RequireDigit = true;
// Lockout settings.
identityOptions.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
identityOptions.Lockout.MaxFailedAccessAttempts = 5;
identityOptions.Lockout.AllowedForNewUsers = true;
// User settings.
identityOptions.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
identityOptions.User.RequireUniqueEmail = true;
}, options => {
options.ConnectionString = "mongodb://localhost:27017/MyDB";
options.UsersCollection = "ApplicationUser";
options.RolesCollection = "clientRole";
}).AddDefaultUI();
// This is required to ensure server can identify user after login
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
});
But I get the following error before compiling:
'MongoIdentityOptions' does not contain a definition for 'Password' and no accessible extension method 'Password' accepting a first argument of type 'MongoIdentityOptions' could be found (are you missing a using directive or an assembly reference?)
I got the code from here.
I tried to comment out all accesses to identityOptions first. But then I get the following error before compiling the code:
'Application.Areas.Identity.Data.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'MongoIdentityExtensions.AddIdentityMongoDbProvider<TUser, TRole>(IServiceCollection, Action, Action)'. There is no implicit reference conversion from 'Application.Areas.Identity.Data.ApplicationUser' to 'AspNetCore.Identity.Mongo.Model.MongoUser'.
What am I doing wrong?
ConfigureServicesmethod). In order to help you, we would probably need to get the whole startup.cs code. However, I found a working demo based on .NET 5: github.com/matteofabbri/AspNetCore.Identity.Mongo/tree/master/…