在MongoDB中使用ASP.NET核心标识 [英] Using ASP.NET Core Identity with MongoDB

查看:92
本文介绍了在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核心标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆