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
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 atSystem.Web.Security.SqlRoleProvider
uses them. BecauseRoles.RoleExists("RoleName")
will search these tables to see if the current user is assigned to theRoleName
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:
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:
Of course, you need to update the code with your own namespace, options, etc...
After that, your code at
Roles.RoleExists("RoleName")
will execute theRoleExists
method from your custom role provider.