I'm upgrading my ASP.Net MVC5 application from Identity 1 to 2.1. After one day I made it run... but I can't verify roles for my users (by IsInRole or Authorize Attribute). I guess it is because MVC doesn't resolve the user and role manager since my application uses Unity DI and Identity 2 seems to be based on OwinContext with the following configuration :
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
Is there a simple way, tutorial or documentation to have Identity 2 working with external dependency injection ?
[Edit] Now Unity DI seems to work a little (thanks to meep).
I modified Startup.Auth.cs, adding to ConfigureAuth(IAppBuilder app) function :
var container = UnityConfig.Container;
var dbContext = container.Resolve<ApplicationDbContext>();
container.RegisterInstance<IAppBuilder>(app, new ContainerControlledLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
container.RegisterType<UserManager<ApplicationUser>, ApplicationUserManager>();
container.RegisterType<IRoleStore<IdentityRole, string>, RoleStore<IdentityRole>>();
container.RegisterType<RoleManager<IdentityRole>, ApplicationRoleManager>();
// [Edit 2]
app.CreatePerOwinContext<ApplicationDbContext>((options, owinContext) => container.Resolve<ApplicationDbContext>());
app.CreatePerOwinContext<UserManager<ApplicationUser>>((options, owinContext) => container.Resolve<UserManager<ApplicationUser>>());
app.CreatePerOwinContext<RoleManager<IdentityRole>>((options, owinContext) => container.Resolve<RoleManager<IdentityRole>>());
In my UnitiConfig.cs file I added the declaration for a Container singleton (used above).
In IdentityConfig.cs, I dropped the Create method and changed the ctor for
public ApplicationUserManager(IUserStore<ApplicationUser> store, ApplicationDbContext dbContext)
: base(store)
{
this.Initialize(dbContext);
}
private void Initialize(ApplicationDbContext dbContext)
{
// Configurer la logique de validation pour les noms d'utilisateur
this.UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// ... more code...
}
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
: base(roleStore)
{
}
}
I can authenticate the users... but UserManager.GetRoles() is always empty. Moreover [Authorize(Roles="myrole"] rejects every user...
ApplicationUserManager,ApplicationRoleManager,ApplicationSignInManagerandApplicationDbContextin your DI container. Beware that Identity still needs Owin registration forApplicationDbContextand forApplicationUserManager, so keep it present. - trailmaxapp.CreatePerOwinContext<ApplicationUserManager>(DependencyResolver.GetService<ApplicationUserManager>());. Not very familiar with Unity, but this works with Autofac and SimpleInjector. - trailmaxUserManager.GetRoles(), can you check if roles are actually assigned in the database? - trailmax