Sea*_*ome 7 asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5 asp.net-identity
我正在使用Identity2.0和MVC5 CodeFirst我已经扩展了这两个IdentityUser
并且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")
{
}
}
Run Code Online (Sandbox Code Playgroud)
通知MyAppDb
继承自IdentityDbContext
但我通过ApplicationRole
而不是IdentityRole
.我ApplicationRole
继承了IdentityRole
这应该不是问题.
但...
我AccountController
正在抛出这个错误:
The entity type IdentityRole is not part of the model for the current context.
Run Code Online (Sandbox Code Playgroud)
在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);
}
Run Code Online (Sandbox Code Playgroud)
在user
这里正在形成:
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
Run Code Online (Sandbox Code Playgroud)
什么是"重大故障"?;)
更新:
我很确定这个问题与UserStore
这里有关:
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; }
...
Run Code Online (Sandbox Code Playgroud)
我怎样UserStore
才能知道什么ApplicationRole
时候我只能传递一个参数?
Sea*_*ome 11
我通过创建ApplicationUserStore
和解决了这个问题ApplicationUserManager
:
public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
[Required]
[StringLength(50)]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
public string LastName { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationUserLogin : IdentityUserLogin<string>
{
}
public class ApplicationUserClaim : IdentityUserClaim<string>
{
}
public class ApplicationUserRole : IdentityUserRole<string>
{
}
public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
[Required]
[StringLength(50)]
public string ProperName { get; set; }
}
public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public static MyAppDb Create()
{
return new MyAppDb();
}
public MyAppDb()
: base("MyAppDb")
{
}
}
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationUserStore(MyAppDb context)
: base(context)
{
}
}
public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options)
{
var manager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()));
// Configure the application user manager
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
return manager;
}
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
{
public ApplicationRoleStore(MyAppDb context)
: base(context)
{
}
}
public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
: base(store)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options)
{
var manager = new ApplicationRoleManager(new ApplicationRoleStore(new MyAppDb()));
return manager;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14109 次 |
最近记录: |