- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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")
{
}
}
MyAppDb
继承自
IdentityDbContext
但我通过了
ApplicationRole
而不是
IdentityRole
.我的
ApplicationRole
继承自
IdentityRole
所以这应该不是问题。
AccountController
正在抛出这个错误:
The entity type IdentityRole is not part of the model for the current context.
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);
}
user
正在这里创建:
var user = await UserManager.FindAsync(model.UserName, model.Password);
if (user != null)
{
await SignInAsync(user, model.RememberMe);
return RedirectToLocal(returnUrl);
}
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; }
...
UserStore
想知道
ApplicationRole
当我只能传递一个参数时?
最佳答案
我通过创建 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;
}
}
关于asp.net-mvc - 为什么 UserManager.CreateIdentityAsync() 寻找 IdentityRole 以及如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22672622/
我将 ASP.NET Identity 2.0 与我自己的自定义商店一起使用。我注意到存储操作被多次低效地调用,尤其是在登录时。 这是我的登录代码(几乎是默认模板中包含的代码): [AllowAnon
我正在尝试修改在 visual studio 2013 sp 3 上创建新的 mvc 项目时自动创建的登录系统。当我注册我的用户时,我会转到 await SignInAsync(user, isPe
如果我做了more than one API调用同时出现错误 A second operation started on this context before a previous asynchro
我创建了一个实现 UserModel 的 ViewModel( IUser ) (用于自定义 ASP.NET Identity 2.0) public class UserModel : IUser
我正在尝试为 MVC5 站点(在 VS2012 中)设置 OAuth。 我正在使用 Fluent NHibernate。我已经设置了我自己的用户存储并传入一个存储库对象来访问 NHibernate s
我正在使用 Identity2.0 与 MVC5 CodeFirst 我已经扩展了 IdentityUser和 IdentityRole像这样: public class ApplicationUse
我正在使用 asp.net mvc core 2.1 版本,在 ApplicationUser 类中添加属性时收到这两个错误我在这段代码上收到错误: var userIdentity = await
我是一名优秀的程序员,十分优秀!