I've created a test project using:
dotnet new razor --auth Individual --output Test
This creates a Startup.cs that contains:
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);
}
I want to seed some users and roles. Both users and roles will use the same store (SQLite). I'm using a static class for seeding which it's called from Program
.
I can seed users, but not roles, since the above does not seem to inject a RoleManager
.
In ASP.NET Core 2.0 the following is used:
services.AddIdentity<IdentityUser, IdentityRole>()
I'm guessing AddDefaultIdentity
is new in 2.1 but the problem is that it does not inject a RoleMnager
, so what should I do?
For role-based authorization, the customer is responsible for providing the user ID, any optional attributes, and all mandatory user attributes necessary to define the user to Payment Feature Services. The customer must also define the roles that are assigned to the user.
ASP.NET offers a Roles framework for defining roles and associating them with user accounts. With the Roles framework we can create and delete roles, add users to or remove users from a role, determine the set of users that belong to a particular role, and tell whether a user belongs to a particular role.
Right click “AspNetRoles” table and click New Query. Step 5: Insert roles in “AspNetRoles” table, using SQL query. The screenshots, given below, explain how to insert the roles in the specified table. Step 6: We need to map with role ID and users ID now.
It seems that finally Microsoft understood that not every application needs roles and separated them.
Notice that AddDefaultIdentity
is declared as:
public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services) where TUser : class;
So, you can continue to configure Identity options through that IdentityBuilder
. What you want to do is:
services.AddDefaultIdentity<IdentityUser>().AddRoles<IdentityRole>();
Fortunately, they also removed the IUser
and IRole
constrains, so now you can use models in a completely separate assembly without having to install hundreds of NuGet packages.
Might help someone else: If you add asp.net identity through scaffolding to an existing project, you'll need to edit the IdentityHostingStartup.cs
and change the services there instead of in your startup class:
services.AddIdentity<AppUser, IdentityRole>()
.AddDefaultUI()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<authContext>();
And then you can use the role manager in your seeding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With