[英]ASP .Net Core Identity Role Claims not adding to User
我在使用角色/索賠時遇到問題。
我已經創建了角色,並給出了角色聲明。 然后將這些角色分配給用戶,根據我在網上閱讀的內容,這意味着用戶應繼承角色聲明,但不能繼承。 該政策無效,經進一步檢查,通過JSON輸出用戶聲明時,我看不到聲明。
如我所見,所有數據都保存在數據庫中。
角色/要求播種者
public static void SeedRolesAndClaims(RoleManager<IdentityRole> roleManager)
{
// Create Roles
IdentityRole adminRole = new IdentityRole("Admin");
roleManager.CreateAsync(adminRole).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "edit.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "delete.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "create.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "view.post")).Wait();
roleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "create.comment")).Wait();
IdentityRole userRole = new IdentityRole("User");
roleManager.CreateAsync(userRole).Wait();
roleManager.AddClaimAsync(userRole, new Claim(ClaimTypes.AuthorizationDecision, "create.comment")).Wait();
}
用戶播種者
ApplicationUser user = new ApplicationUser { UserName = "john@email.com", FirstName = "Admin", LastName = "Smith", Email = "john@email.com" };
userManager.CreateAsync(user, "Password123*").Wait();
userManager.AddToRoleAsync(user, "Admin").Wait();
我正在做的檢查是
var claims = User.Claims.Select(claim => new { claim.Type, claim.Value }).ToArray();
return Json(claims);
它返回用於身份驗證的基本JSON聲明
[{"type":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier","value":"05bef53e-dd97-41f6-beee-531501cf8598"},{"type":"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name","value":"john@email.com"},{"type":"AspNet.Identity.SecurityStamp","value":"SGS23ZGIY6UYOOL2APWRIZKNT2V6QBJC"}]
我不確定是什么問題,並且已經在google / stackoverflow上搜索了一段時間而沒有成功。
任何幫助將不勝感激
如果使用的是.Net Core 2.1,則似乎需要根據此問題更改默認的Identity配置。
在.Net Core 2.1中,您可以首先創建自己的ApplicationUser
:
public class ApplicationUser : IdentityUser
{
}
修改您的dbcontext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
使用舊式api配置身份:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
並播種用戶和角色,例如:
private async Task CreateUserRoles(IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
IdentityResult roleResult;
//Adding Admin Role
var roleCheck = await RoleManager.RoleExistsAsync("Admin");
if (!roleCheck)
{
IdentityRole adminRole = new IdentityRole("Admin");
//create the roles and seed them to the database
roleResult = await RoleManager.CreateAsync(adminRole);
RoleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "edit.post")).Wait();
RoleManager.AddClaimAsync(adminRole, new Claim(ClaimTypes.AuthorizationDecision, "delete.post")).Wait();
ApplicationUser user = new ApplicationUser { UserName = "v-nany@hotmail.com", Email = "v-nany@hotmail.com" };
UserManager.CreateAsync(user, "xxxxxx").Wait();
await UserManager.AddToRoleAsync(user, "Admin");
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env ,IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
CreateUserRoles(serviceProvider).Wait();
}
最后,注銷並重新登錄帳戶,索賠應該在以下位置:
如果使用的是.Net Core 2.0,則您的代碼應使用默認的身份模板。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.