A9 Security: Authorization (ApplicationRoleManager) (Wpf, Xamarin) - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki

To add a custom ApplicationRoleManager implementation to your project, follow these steps:

  • With Visual studio 2019 open Dm04WebApp.csproj project
  • open App_Start\IdentityConfig.cs-file
  • insert the code below right after the definition of ApplicationUserManager {...}-class
    public class ApplicationRoleManager : RoleManager<IdentityRole>
    {
        public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore) : base(roleStore)
        {
        }

        public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
        {
        ///It is based on the same context as the ApplicationUserManager
            var appRoleManager = 
                new ApplicationRoleManager(new RoleStore<IdentityRole> (context.Get<ApplicationDbContext> ()));
            return appRoleManager;
        }
    }
  • open App_Start\Startup.Auth.cs-file
  • find ConfigureAuth(IAppBuilder app){ ... }-method.
  • Place the cursor immediately after the line of code: app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
           public void ConfigureAuth(IAppBuilder app)
            {
                ...
                app.CreatePerOwinContext(ApplicationDbContext.Create);
                app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
                --PLACE CURSOR HERE--
  • Insert the following line of code: app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
  • Here is a result
            public void ConfigureAuth(IAppBuilder app)
            {
                ...
                app.CreatePerOwinContext(ApplicationDbContext.Create);
                app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
                app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
                ...