0

I was using the following code to add roles of the user.

 Roles.AddUserToRole(model.Email, "COMPANYVIP"); 

and then i got this error:

 The Role Manager feature has not been enabled 

after some research i found out that we have to add the following connection string in web.config

 <configuration> <system.web> <roleManager enabled="true" /> </system.web> </configuration> 

adding this eliminated my first error but now i get this error:

 A network-related or instance-specific error occurred while establishing a connection to SQL Server 

what should i do now?

1 Answer 1

1

Remove your change in web.config and in Startup.Auth add the following reference to ConfigureAuth:

public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(ApplicationDbContext.Create); // Add this reference to RoleManager (without changing any other items) // Make sure it is added below ApplicationDbContext.Create app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create); } 

Then in your Controller, make sure it includes this in the constructor:

public class YourController : Controller { // Add this private ApplicationRoleManager _roleManager; // Add roleManager public YourController(ApplicationRoleManager roleManager) { // Add this RoleManager = roleManager; } public ApplicationRoleManager RoleManager { get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); } private set { _roleManager = value; } } } 

and also include this in the Controller's Dispose (if you have it):

protected override void Dispose(bool disposing) { if (disposing) { // include this if (_roleManager != null) { _roleManager.Dispose(); _roleManager = null; } } base.Dispose(disposing); } 

You may also need to add this code to IdentityConfig (in the App_Start folder if you're using the template):

public class ApplicationRoleManager : RoleManager<IdentityRole> { public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore) : base(roleStore) { } public static ApplicationRoleManager Create( IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) { var manager = new ApplicationRoleManager( new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>())); return manager; } } 

You should now be able to use the RoleManager in the Controller.

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for your reply. but as i was performing as you said. there was no ApplicationRoleManger class in my library. i searched and got this link link and then i added the class manually as suggested. and now i get another error like this *An exception of type 'System.ArgumentNullException' occurred in Microsoft.AspNet.Identity.EntityFramework.dll but was not handled in user code where did i do wrong?
@Yogesh Gautam I've updated the answer to include reference to the RoleManager definition
Again i get the "value cannot be null error" on implementing var manager = new ApplicationRoleManager( new RoleStore<IdentityRole> (context.Get<ApplicationDbContext>()));
The above problem is solved. my mistake was that i added applicationrolemanage in ConfigureAuth method before creating the context . so it was showing me null value .
But again my problem is still not solved while adding user to role it gets disposed and shows me this error "The Role Manager feature has not been enabled." .
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.