我在用着身份2.0 with MVC5 代码优先我已经延长了IdentityUser
and IdentityRole
像这样:
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
继承自IdentityDbContext
但我通过了ApplicationRole
代替IdentityRole
. My ApplicationRole
继承自IdentityRole
所以这应该不是问题。
BUT...
My AccountController
抛出这个错误:
The entity type IdentityRole is not part of the model for the current context.
At the UserManager.CreateIdentityAsync(...)
在这段代码中:
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
正在这里创建:
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
什么是“重大故障”? ;)
UPDATE:我相当确定这个问题与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; }
...
我如何获得UserStore
了解ApplicationRole
当我只能传递一个参数时?