![](/img/trans.png)
[英]How do I seed user roles in Identity ASP.NET Core 2.1 (Identity Scaffolded)
[英]How to use Roles in ASP.NET Core 2.1?
我使用以下方法創建了一個測試項目:
dotnet new razor --auth Individual --output Test
這將創建一個包含以下內容的 Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
我想播種一些用戶和角色。 用戶和角色都將使用相同的存儲 (SQLite)。 我正在使用 static class 進行播種,它是從Program
調用的。
我可以播種用戶,但不能播種角色,因為上面似乎沒有注入RoleManager
。
在 ASP.NET Core 2.0 中使用了以下內容:
services.AddIdentity<IdentityUser, IdentityRole>()
我猜AddDefaultIdentity
是 2.1 中的新功能,但問題是它沒有注入RoleMnager
,所以我該怎么辦?
微軟似乎終於明白並不是每個應用程序都需要角色並將它們分開。
請注意, AddDefaultIdentity
聲明為:
public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services) where TUser : class;
因此,您可以繼續通過IdentityBuilder
配置 Identity 選項。 你想要做的是:
services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>();
幸運的是,他們還刪除了IUser
和IRole
約束,因此現在您可以在完全獨立的程序集中使用模型,而無需安裝數百個 NuGet 包。
可能對其他人有幫助:如果您通過腳手架向現有項目添加 asp.net 身份,則需要編輯IdentityHostingStartup.cs
並更改那里的服務,而不是在您的啟動類中:
services.AddIdentity<AppUser, IdentityRole>()
.AddDefaultUI()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<authContext>();
然后您可以在播種中使用角色管理器。
除了已經提供的答案之外,盡管添加了.AddRoles<Identity>()
,但在我的控制器上使用Authorize(Roles = "Administrator")
時,我仍然無法獲得授權。 出於某種原因,“角色聲明似乎不會影響具有角色名稱的 IsUserInRole 或 AuthorizeAttribute”。
為了利用角色,我建議使用 ASP.NET 2.0 方式,如下所示:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
這樣,您就可以使用您的角色,並為您搭建身份頁面。
在aspnet github上參考這個問題: Issue 1813
從 .Net Core 2.1 開始, AddDefaultIdentity
與調用相同:
AddIdentity
AddDefaultUI
AddDefaultTokenProviders
要添加角色功能,請轉到ConfigureServices
下的Startup.cs
,您可以像這樣使用.AddRoles
:
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>() //<-- This line
.AddEntityFrameworkStores<ApplicationDbContext>();
這就是所需要的。 注銷並重新登錄至關重要。
為了記錄(並且只是為了測試),我嘗試了services.AddIdentity
:
IServiceCollection 不包含“AddIdentity”的定義...
和services.AddIdentityCore
(在調試和顯示頁面之前沒有錯誤):
InvalidOperationException: 未指定 authenticationScheme,也未找到 DefaultChallengeScheme。 可以使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action configureOptions) 設置默認方案。
可能還有更多方法可以使后兩者正常工作,但我為AddDefaultIdentity
發布的代碼是我所需要的,以便讓User.IsInRole
和其他角色功能在 .NET Core 2.1(和 3.1)中工作。
不久前我也與這個問題作斗爭。 我無法讓角色發揮作用。
在我的項目中,我使用了 ASP.NET Core 2.1(它可以在 2.2 中修復,請參閱鏈接)並且還搭建了一些頁面。
在互聯網上長時間搜索后,我找到了這個解決方案(在上面的Issue 1813 中也提到過)
解決方案是我(如文章中所提議的)在 IUserClaimsPrincipalFactory 中添加以下行:
services.AddScoped<IUserClaimsPrincipalFactory<IdentityUser>,
UserClaimsPrincipalFactory<IdentityUser, IdentityRole>>();
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<TranslatorDbContext>();
另請注意,此代碼是順序敏感的。 如果您將 AddRoles 放在 Add EntityFrameworkStore 之后,您將得到一個異常。
所以確保順序是這樣的:
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.