[英]How to implement ASP.NET Core 3.1 Identity with MongoDB?
[英]Using ASP.NET Core Identity with MongoDB
我目前正在嘗試結合 MongoDB 學習 ASP.NET Core 3.1。 我已經可以執行簡單的 CRUD 操作了。 現在我想設置與 MongoDB 相關的 ASP.NET 核心標識。 為此,我安裝了以下 Nuget 軟件包:
在 Configure 方法的 IdentityHostingStartup class 中,我現在正在執行以下代碼:
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”不包含“密碼”的定義,並且找不到接受“MongoIdentityOptions”類型的第一個參數的可訪問擴展方法“密碼”(您是否缺少 using 指令或程序集引用?)
我從這里得到了代碼。
我試圖首先注釋掉對 identityOptions 的所有訪問。 但是在編譯代碼之前我收到以下錯誤:
“Application.Areas.Identity.Data.ApplicationUser”不能用作泛型類型或方法“MongoIdentityExtensions.AddIdentityMongoDbProvider<TUser, TRole>(IServiceCollection, Action, Action)”中的類型參數“TUser”。 沒有從“Application.Areas.Identity.Data.ApplicationUser”到“AspNetCore.Identity.Mongo.Model.MongoUser”的隱式引用轉換。
我究竟做錯了什么?
我也遇到了同樣的情況,結果證明我混淆了README.md為 AspNetCore.Identity.Mongo 提供的示例。 為了創建我的 ApplicationUser 我已經按照示例使用不同的主鍵類型,所以它看起來像這樣:
public class ApplicationUser : MongoUser<string>
{
// Additional properties
}
因此在 ConfigureServices 中需要以下內容(注意不同的類型參數):
services.AddIdentityMongoDbProvider<ApplicationUser, ApplicationRole, string>(identity =>
{
identity.Password.RequiredLength = 8;
// other options
},
mongo =>
{
mongo.ConnectionString = "mongodb://127.0.0.1:27017/identity";
// other options
});
事實證明,我不需要使用不同的主鍵類型。 因此,我刪除了它,並且能夠使用 AddIdentityMongoDbProvider 方法,該方法僅具有 User 和 Role 的類型參數,就像您在示例中所做的那樣。 刪除主鍵類型參數后,我的 ApplicationUser 看起來像這樣:
public class ApplicationUser : MongoUser
{
// Additional properties
}
我真的不敢相信每個人都在沒有啟用安全性的情況下使用 MongoDb。 我花了很多時間試圖讓它與安全的 URL 一起工作
mongodb://superuser:changeMeToAStrongPassword@localhost:27017/?authSource=admin&authenticationDatabase=admin
因此,對於 MongoDbGenericRepository,有必要將數據庫名稱傳遞給 ctor,對於身份提供者,它應該在第一個 / 之后的字符串中。
這對我有幫助並且適用於這兩種情況,因此我可以為 Identity 重新定義 dbName 並將其與 Repository 一起使用
mongodb://superuser:changeMeToASstrongPassword@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");
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.