UserRoles Not Working In Asp.Net

925 views Asked by At

I am currently trying to get the UserRoles to work in ASP.NET. I am facing some issues. For some reason it doesn't seem to pick up the roles from the DB.

I am changing the table name to meet my requirement by using the following code in IdentityModels.cs

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUserRole>().ToTable("MyRroles");
    }

At First it couldn't find the schema and after doing some research I followed this article and that particular error disappeared. However following the article it added the following tables in my DB

aspnet_Applications
aspnet_Roles
aspnet_SchemaVersions
aspnet_Users
aspnet_UsersInRoles

Which I don't need. I'm doing a basic if statement to check whether the roles are working of not. And it doesn't seem to pick up the roles.

if(Roles.RoleExists("RoleName"))
        {
            Button1.Visible = false;
    }

In my web.config file I have the following code

<roleManager defaultProvider="MyConn" enabled="true">
        <providers>
            <add
              name="MyConn"
              type="System.Web.Security.SqlRoleProvider"
              connectionStringName="MyConn"
              applicationName="MyApp"/>
        </providers>
    </roleManager>

Can anyone tell me where I am going wrong please. Thank you for all your help and support

1

There are 1 answers

9
SmartDev On BEST ANSWER

If you want to do a basic if statement to check whether the roles are working of not then you need to store the roles somewhere, right? And since you want to use the default role provider you need these SQL tables. The default provider at System.Web.Security.SqlRoleProvider uses them. Because Roles.RoleExists("RoleName") will search these tables to see if the current user is assigned to the RoleName role.

If you want alter the way a user is assigned to a role, then you need to implement your own role provider.

Here is an example:

public class MyRoleProvider : RoleProvider
{
    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        // your logic here
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        // your logic here
    }

    public override string[] GetAllRoles()
    {
        // your logic here
    }

    public override bool RoleExists(string roleName)
    {
        // your logic here
    }

    // etc...
 }

Check this article for all the available methods: http://msdn.microsoft.com/en-us/library/8fw7xh74(v=vs.100).aspx.

Then, in order to setup your application to use your provider simply update your web.config file:

<roleManager defaultProvider="MyRoleProvider" enabled="true" cacheRolesInCookie="true" cookieName=".ASPROLES" cookieTimeout="3" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All">
    <providers>
        <clear />
        <add name="MyRoleProvider" type="MyNamespace.MyRoleProvider" connectionStringName="DefaultConnection" applicationName="MyAppName" writeExceptionsToEventLog="false" />
    </providers>
</roleManager>

Of course, you need to update the code with your own namespace, options, etc...

After that, your code at Roles.RoleExists("RoleName") will execute the RoleExists method from your custom role provider.