December 11, 2015 00:34 by
Peter
There is something great included in ASP.NET MVC 5 that looks underutilized except by the ASP.NET MVC team. the rest of us seem to be ignoring it, that is apparent when gazing the solutions on StackOverflow (and blogs) for questions like “how do i use roles with ASP.NET MVC 5?” once I check out how identity is implemented in mvc, what stands out is that the dependency injection. And, that dependency injection seems to be missing in the answers to how to implement roles. Dependency is a great tool, it’s built into the OWIN implementation, so why not use it?
To implement the role manager in MVC 5, look for a file in App_Start called IdentityConfig.cs. If you don’t have this file, look for the file that contains the implementation of your ApplicationUserManager (derived from UserManager) and ApplicationSignInManager (derived from SignInManager). At the bottom of that file, within the namespace, add the subsequent class definition:
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> store) : base(store)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
var roleStore = new RoleStore<IdentityRole>(context.Get<AuthenticationDbContext>());
return new ApplicationRoleManager(roleStore);
}
}
There are a couple of prototypes for the produce method. during this case, I needed to urge the database context from the OWIN context, so I used the more elaborate overload. If you don’t want to do that, you can use the Create(Func<T>) overload, which doesn’t take any parameters. Now, to make sure OWIN knows about your ApplicationRoleManager class, go the ConfigureAuth method in your Startup partial class implementation in Startup.Auth.cs (also in App_Start). Or, you'll also put what I’m getting ready to show you in the Configuration method of the OWIN startup class. This class is sometimes called Startup.cs and you’ll find it in the same directory as the root web.config.
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(AuthenticationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
…
}
See how I just stuck that line for ApplicationRoleManager there next to the other authentication delegate registrations? Now, we can just inject the role manager right into the AccountController or whatever other controller we need.
public AccountController(
SendMailManager emailManager,
ApplicationUserManager userManager,
ApplicationSignInManager signInManager,
ApplicationRoleManager roleManager)
{
this.MailManager = emailManager;
this.RoleManager = roleManager;
this.SignInManager = signInManager;
this.UserManager = userManager;
}
As you can see, I have already used the dependency injection to insert my own mail manager. AccountController should already be set up with this kind of structure. If it’s not, though, simply changing your constructor to look like this will cause MVC to inject those dependencies into your controller. Then, all you have to do is create a property to hold that ApplicationRoleManager thing and you’re all set!
private ApplicationRoleManager roleManager;
public ApplicationRoleManager RoleManager
{
get
{
return this.roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
}
private set { this.roleManager = value; }
}
HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.