C# 如何使用MongoDB实现ASP.NET Core 3.1身份?
是一个API,用于简化后端和逻辑代码,以管理用户、密码、配置文件数据、角色、声明、令牌、电子邮件确认等 对于VisualStudio,它需要提供多个模板页面,但需要依赖的C# 如何使用MongoDB实现ASP.NET Core 3.1身份?,c#,mongodb,asp.net-identity,asp.net-core-3.1,C#,Mongodb,Asp.net Identity,Asp.net Core 3.1,是一个API,用于简化后端和逻辑代码,以管理用户、密码、配置文件数据、角色、声明、令牌、电子邮件确认等 对于VisualStudio,它需要提供多个模板页面,但需要依赖的DataContext,后者目前支持CosmosDB For Core 3.1,这是唯一的NoSQL数据库 有哪些选项可用于实现ASP.NET核心标识并允许搭建?使用公开的Mongo Identity NuGet软件包替换默认的ASP.NET核心标识。少数仍在维护中的软件包是和 在NuGet中安装最新的软件包(请参见上文) 右
DataContext
,后者目前支持CosmosDB For Core 3.1,这是唯一的NoSQL数据库
有哪些选项可用于实现ASP.NET核心标识并允许搭建?使用公开的Mongo Identity NuGet软件包替换默认的ASP.NET核心标识。少数仍在维护中的软件包是和
使用公开的Mongo Identity NuGet软件包替换默认的ASP.NET核心标识。少数仍在维护中的软件包是和
嗨,我正试着做隔离,但我在构建时遇到了一个错误,上面写着“'TestIdentityMongo.Areas.Identity.Data.TestIdentityMongoUser'不能用作泛型类型或方法'IdentityDbContext'中的类型参数'TUser'。没有从“TestIdentityMongo.Areas.Identity.Data.TestIdentityMongoUser”到“Microsoft.AspNetCore.Identity.Identity.IdentityUser”的隐式引用转换。“我不知道我是不是在做坏事。看起来MongoUser类现在已经过时了。Hello@ste.xin,我不确定,但您可以尝试其他选择,例如,我将编辑此答案以反映我发现的更好方法。我正在尝试使用mongoDB和Identity设置Blazor服务器应用程序。但是当我粘贴“IdentityHostingStartup.cs”文件的代码时,我总是会得到错误:“iservicecollection不包含“addidentitymongodbprovider”的定义。我做错了什么?嗨,我正试图按seggested做,但我在构建时遇到一个错误,它说”“TestIdentityMongo.Areas.Identity.Data.TestIdentityMongoUser”不能用作泛型类型或方法“IdentityDbContext”中的类型参数“TUser”。没有从“TestIdentityMongo.Areas.Identity.Data.TestIdentityMongoUser”到“Microsoft.AspNetCore.Identity.Identity.IdentityUser”的隐式引用转换。“我不知道我是不是在做坏事。看起来MongoUser类现在已经过时了。Hello@ste.xin,我不确定,但您可以尝试其他选择,例如,我将编辑此答案以反映我发现的更好方法。我正在尝试使用mongoDB和Identity设置Blazor服务器应用程序。但是当我粘贴“IdentityHostingStartup.cs”文件的代码时,我总是会得到一个错误:“iservicecollection不包含“addidentitymongodbprovider”的定义。我做错了什么?
// Add Identity for AspNetCore.Identity.Mongo, ApplicationRole is optional
services.AddIdentityMongoDbProvider<ApplicationUser, ApplicationRole>(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;
}, mongoIdentityOptions => {
mongoIdentityOptions.ConnectionString = "mongodb://localhost:27017/MyDB";
// mongoIdentityOptions.UsersCollection = "Custom User Collection Name, Default User";
// mongoIdentityOptions.RolesCollection = "Custom Role Collection Name, Default Role";
}).AddDefaultUI(); //.AddDefaultUI() to temporary remove error when no EmailSender provided, see https://stackoverflow.com/questions/52089864/
// 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;
});
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// code here...
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// add app.UseAuthentication(); to register authentication service, without it, user could technically login but has no logged in session created.
app.UseAuthentication();
app.UseAuthorization();
// more code
}