I\'m having issues with using Role/Claims.
I have created Roles and given the roles claims. Then assigned these roles to the users, from what I read online this mean
If you are using .Net Core 2.1 , it seems you will need to change the default Identity configuration according to this issue .
In .Net Core 2.1 , you could firstly create your own ApplicationUser
:
public class ApplicationUser : IdentityUser
{
}
Modify your dbcontext :
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
}
Configure the identity using the old-style api :
services.AddDbContext(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity()
.AddRoleManager>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores();
And seed the user and role like :
private async Task CreateUserRoles(IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService>();
var UserManager = serviceProvider.GetRequiredService>();
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();
}
At last ,logout and re-signin the account , the claim should be there :
If you are using .Net Core 2.0 , your code should work with the default identity template .