1

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?

Seraphim
  • 37
  • 7

2 Answers2

0

You got to configure the default role provider/membership provider in web.config and run aspnet_regsql.exe against your database.

here is the documentations: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-security/membership/creating-the-membership-schema-in-sql-server-cs

  <system.web>
    <roleManager enabled="true" cacheRolesInCookie="false" defaultProvider="AspNetSqlRoleProvider">
      <providers>
        <clear />
        <add applicationName="app-name" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="roleManagerSqlConnString" />
        <add applicationName="app-name" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
      </providers>
    </roleManager>
  </system.web> 
using System.Web.Security;

                string userName = HttpContext.Current.User.Identity.Name;
                if (!Roles.IsUserInRole(roleName))
                {
                    Roles.AddUserToRole(userName, roleName);
                }
  • Thats what I've found on google. I've done that before, so I get new tables e.g. aspnet_Users instead of AspNetUsers. But actual the whole page is configured to use the old schematics. As I understand correct, it looks like I configure the Identity Module with the aspnet_regsql.exe and not the old Membership Module that is acually used. – Seraphim Feb 13 '19 at 05:55
  • I don't want to replace the existing User Management by the version that's created by aspnet_regsql.ese because I already have the User Management running and only want to activate the role management. The way you showing is what you can find everywhere, but for this I have to migrate the whole existing data to the new schematics. – Seraphim Feb 14 '19 at 17:33
  • I think role management provider and membership provider are two separate things. You don't have to use the membership/profile portion of what aspnet_regsql.exe created. Just change the web.config to use your preferred membership provider like you would on the role provider. – Anh-Tuan N. Feb 14 '19 at 22:54
  • Ok, but how? The way you showed doesn't work, because if I try to add a role (Roles.CreateRole()) I've been asked for a function according to the new schematic – Seraphim Feb 15 '19 at 05:51
0

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

Seraphim
  • 37
  • 7