AspNet.Identity 自定义用户和自定义角色应该很简单;我错过了啥?
Posted
技术标签:
【中文标题】AspNet.Identity 自定义用户和自定义角色应该很简单;我错过了啥?【英文标题】:AspNet.Identity Custom User and Custom Role should be simple; what am I missing?AspNet.Identity 自定义用户和自定义角色应该很简单;我错过了什么? 【发布时间】:2014-05-08 09:32:44 【问题描述】:使用http://www.asp.net/identity 中的示例我已经做到了这一点。 RoleManager
工作完美,我对待 UserManager
也是一样的。我认为一切都是正确的,但我似乎无法在控制器中正确地新建 UserManager
。怎么了?有一次,我成功地让UserManager
工作,但是在使用UserManager.Create(user, password);
创建一个新用户时得到一个EntityValidationError
说“需要ID”,正如在这个问题UserManager.Create(user, password) thowing EntityValidationError saying Id is required? 中发布的那样。
因此,经过一段时间的碰碰运气,我创建了如下所示的所有内容,但在 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;
...
这是模型:
namespace MyApp.Models
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(UserManager<ApplicationUser> 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;
[Required]
public string Description get; set;
public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
public MyAppDb()
: base("MyAppDb")
public class ApplicationUserManager : UserManager<ApplicationUser>
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
this.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
this.UserValidator = new UserValidator<ApplicationUser>(this) AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true ;
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
public ApplicationUserStore(MyAppDb context)
: base(context)
public override async Task CreateAsync(ApplicationUser user)
await base.CreateAsync(user);
public class ApplicationRoleStore : RoleStore<ApplicationRole, string, ApplicationUserRole>
public ApplicationRoleStore(MyAppDb context)
: base(context)
public class ApplicationRoleManager : RoleManager<ApplicationRole>
public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
: base(store)
更新:我可以通过更改以下内容来消除创建 UserManager
时出现的错误:
public class ApplicationUserManager : UserManager<ApplicationUser>
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
this.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
this.UserValidator = new UserValidator<ApplicationUser>(this) AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true ;
到这里:
public class ApplicationUserManager : UserManager<ApplicationUser>
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
: base(store)
this.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
this.UserValidator = new UserValidator<ApplicationUser>(this) AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true ;
注意我刚刚添加了, string
,但随后出现错误“Microsoft.AspNet.Identity.UserMaagerbase(store)
上有一些无效参数。
更新 2:我改变了这个:
public class ApplicationUserManager : UserManager<ApplicationUser>
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
...
到这里:
public class ApplicationUserManager : UserManager<ApplicationUser, string>
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
...
注意public class ApplicationUserManager : UserManager<ApplicationUser, string>
中的' string
。但现在,你猜怎么着?你猜对了——回到这个问题:UserManager.Create(user, password) thowing EntityValidationError saying Id is required?
我错过了什么?
【问题讨论】:
UserManager.Create(user, password) thowing EntityValidationError saying Id is required?的可能重复 【参考方案1】:试试这个方法。我有同样的问题,你需要提供身份证。
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
if (ModelState.IsValid)
var user = new ApplicationUser()
UserName = model.UserName,
Id = Guid.NewGuid().ToString(),
Created = DateTime.Now,
LastLogin = null
;
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
else
AddErrors(result);
// If we got this far, something failed, redisplay form
return View(model);
【讨论】:
以上是关于AspNet.Identity 自定义用户和自定义角色应该很简单;我错过了啥?的主要内容,如果未能解决你的问题,请参考以下文章
Microsoft.AspNet.Identity 的自定义成员身份 - CreateLocalUser 失败
AspNet Identity Core - 登录时的自定义声明
ASP.NET Identity - 将用户 ID 主键默认类型从字符串更改为 int 以及使用自定义表名时出错
实体类型“Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>”需要定义一个键