Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling and populating roles seems overly complicated and poorly documented #31

Open
SoftCircuits opened this issue Jun 8, 2020 · 0 comments

Comments

@SoftCircuits
Copy link

Setting up roles seems to be a lot harder than I think it should be. Using code first, it appears the tables related to roles were created. But understanding how to enable and populate them is frustrating.

I've found numerous articles explaining how to do it and everyone of them is different. None of the Microsoft articles seems specific to this.

Based on what I've read, I end up with something like this in startup.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));

    services.AddDefaultIdentity<ApplicationUser>(options =>
        {
            options.SignIn.RequireConfirmedAccount = false;
        })
        .AddRoles<ApplicationUser>()
        .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddRazorPages();

    // Set the default authentication policy to require users to be authenticated
    services.AddControllers(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
    });
}

But the call to AddDefaultIdentity raises an exception.

System.InvalidOperationException: 'AddEntityFrameworkStores can only be called with a role that derives from IdentityRole.'

I just want to enable roles and prepopulate the roles with the ones I'm using. Is there no straight forward way documented to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant