使用http://www.asp.net/identity中的示例我已经走到了这一步.这些RoleManager
作品完美无瑕,我对待它UserManager
也是如此.我认为一切都是正确的,但我似乎无法UserManager
在控制器中正确地创新.怎么了?有一次,我成功地UserManager
开始工作,但是EntityValidationError
在创建一个新用户时发出了一个说法"Id is required",UserManager.Create(user, password);
在这个问题中发布了UserManager.Create(用户,密码)thowing EntityValidationError说Id是必需的?
所以经过一段时间的点击和未命中,我已经创建了如下所示的所有内容但是在编写时错误new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()))
说"最好的重载方法匹配'MyApp.Models.ApplicationUserManager.ApplicationUserManager(Microsoft.AspNet.Identity. IUserStore)'尝试在我的控制器中创建'UserManager'时'有一些无效的参数':
这是控制器:
namespace MyApp.Controllers
{
[Authorize]
public class AccountController : BaseController
{
public AccountController()
: this(new ApplicationUserManager(new ApplicationUserStore(new MyAppDb())))
{
}
public AccountController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
public ApplicationUserManager UserManager { get; private set; }
...
}
Run Code Online (Sandbox Code Playgroud)
这是模型:
namespace MyApp.Models
{
public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
[Required]
[StringLength(50)] …
Run Code Online (Sandbox Code Playgroud) asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5 asp.net-identity
使用Identity 2.0注册用户后,在数据库中创建用户,Account控制器中有代码创建身份并在用户签名.这是我收到错误的时候.以下是产生错误的代码:
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)
这是错误:
实体类型IdentityRole不是当前上下文的模型的一部分.
我的模型看起来像这样:
namespace MyApp.Models
{
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) asp.net-mvc entity-framework entity-framework-5 asp.net-identity asp.net-mvc-5.1
asp.net-mvc ×2