asp.netwebformsasp.net-roles

Asp.net WebForms Enable Roles


I've created a Webpage via Visual Studio 2017 using ASP.NET WebForms.

I'm using the build-in User Management and created some users.

Now I want to use the Role-Function.

First I've enabled the roleManager, but then I get a new error "Could not find stored procedure 'dbo.aspnet_CheckSchemaVersion"

I've tried to use the aspnet_regsql.exe to add the necessary parts to the database, but now I have two kind of tables in my database: AspNetUsers and aspnet_Users.

What Do I have to do, to enable roles with the old AspNet-Schematic?


Solution

  • Standard-WebForms created with VisualStudio using Microsoft.AspNet.Identity so also the Database is created to use the Schematic of Microsoft.AspNet.Identity.

    Therefore you have to use the RoleManager to add Roles:

    string roleName = "role";
    var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
    
    if (!roleManager.RoleExists(roleName))
    {
        var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
        role.Name = TextBoxRole.Text;
        roleManager.Create(roleName);
    }
    

    System.Web.Security has it's own Schematic and does not work with Microsoft.AspNet.Identity