Using ASP.NET Core Identity with MongoDB(在MongoDB中使用ASP.NET核心标识)
问题描述
我目前正在尝试结合MongoDB学习ASP.NET Core 3.1。我已经可以执行简单的CRUD操作了。现在,我想设置与MongoDB相关的ASP.NET核心身份。为此,我安装了以下Nuget包:
- AspNetCore.Identity.Mongo(8.1.0版)
- AspNetCore.Identity.MongoDbCore(3.1.1版)
在Configure方法的IdentityHostingStartup类中,我现在执行以下代码:
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;
});
});
但我在编译前收到以下错误:
""MongoIdentityOptions""不包含""password""的定义,并且找不到接受类型为""MongoIdentityOptions""的第一个参数的可访问扩展方法""password""(您是否缺少Using指令或程序集引用?)"
我从here获取代码。
我先尝试注释掉了对身份选项的所有访问。但是在编译代码之前,我得到了以下错误:
‘Application.Areas.Identity.Data.ApplicationUser’不能用作泛型类型或方法‘MongoIdentityExtensions.AddIdentityMongoDbProvider<;TUser,trole>;(IServiceCollection,Action,Action)中的类型参数’TUser‘。不存在从‘Application.Areas.Identity.Data.ApplicationUser’到‘AspNetCore.Identity.Mongo.Model.MongoUser’.
的隐式引用转换
我做错了什么?
推荐答案
我真不敢相信每个人都在使用没有启用安全保护的MongoDB。 我花了很多时间试图让它与安全URL一起工作,如
mongodb://superuser:changeMeToAStrongPassword@localhost:27017/?authSource=admin&authenticationDatabase=admin
因此,对于MongoDbGenericRepository,必须将数据库名称传递给ctor,而对于身份提供程序,它应该位于First/之后的字符串中。
这对我很有帮助,也适用于这两种情况,因此我可以重新定义身份的DBNAME并将其与Repository一起使用
mongodb://superuser:changeMeToAStrongPassword@localhost:27017/{0}?authSource=admin&;authenticationDatabase=admin
然后像这样
services.AddIdentityMongoDbProvider<User, MongoRole<Guid>, Guid>(options =>
{
options.Password.RequireDigit = true;...
},
mongo =>
{
mongo.ConnectionString = string.Format(
Configuration.GetConnectionString("MongoDb"),
"myProject_users");
})
这篇关于在MongoDB中使用ASP.NET核心标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在MongoDB中使用ASP.NET核心标识


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01