I'm using Identity2.0 with MVC5 CodeFirst I have extended both the IdentityUser
and IdentityRole
like this:
public class ApplicationUser : IdentityUser
{
[Required]
[StringLength(50)]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
public string LastName { get; set; }
}
public class ApplicationRole : IdentityRole
{
[Required]
[StringLength(50)]
public string ProperName { get; set; }
[Required]
public string Description { get; set; }
}
public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public MyAppDb()
: base("MyAppDb")
{
}
}
Notice MyAppDb
inherits from IdentityDbContext
but that I pass an ApplicationRole
instead of IdentityRole
. My ApplicationRole
inherits from IdentityRole
so this should not be a problem.
BUT...
My AccountController
is throwing this error:
The entity type IdentityRole is not part of the model for the current context.
At the UserManager.CreateIdentityAsync(...)
in this code:
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
The user
is being created here:
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
What is the "major malfunction"? ;)
UPDATE:
I'm fairly sure the issue has something to do with the UserStore
here:
public class AccountController : Controller
{
public AccountController ()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyAppDb())))
{
}
public AccountController (UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
UserManager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false };
}
public UserManager<ApplicationUser> UserManager { get; private set; }
...
How do I get the UserStore
to know about ApplicationRole
when I can only pass it one argument?
I solved this issue by creating an
ApplicationUserStore
andApplicationUserManager
: