AspNet.Identity 自定义用户和自定义角色应该很简单;我错过了什么? [英] AspNet.Identity Custom User and Custom Role should be simple; what am I missing?
问题描述
使用 http://www.asp.net/identity 中的示例我已经知道了远的.RoleManager
工作完美,我对待 UserManager
相同.我认为一切都是正确的,但我似乎无法在控制器中正确地新建一个 UserManager
.怎么了?有一次,我成功地让 UserManager
工作,但得到一个 EntityValidationError
说需要 ID".使用 UserManager.Create(user, password);
创建新用户时,如本问题 UserManager.Create(user, password) thowing EntityValidationError 说 Id 是必需的?
Using examples from http://www.asp.net/identity I've gotten this far. The RoleManager
works flawlessly and I treat the UserManager
the same. I think everything is correct, but I can't seem to new up a UserManager
correctly in a controller. What is wrong? At one point, I was successfully getting the UserManager
to work but was getting an EntityValidationError
saying "Id is required" when creating a new user with UserManager.Create(user, password);
as posted in this question UserManager.Create(user, password) thowing EntityValidationError saying Id is required?
因此,经过一段时间的碰碰运气,我创建了如下所示的所有内容,但在 new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()))
上出现编译时错误:
So after some time of hit and miss, I've created everything like the following but am getting a compile-time error on new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()))
saying:
MyApp.Models.ApplicationUserManager.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore
The best overloaded method match for MyApp.Models.ApplicationUserManager.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore<MyApp.Models.ApplicationUser>)' has some invalid arguments"
尝试在我的控制器中创建 UserManager
时:
when trying to create the UserManager
in my controller:
这里是控制器:
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
的错误:
UPDATE: I can get the error to go away on the creation of the UserManager
by changing this:
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.UserMaager<MyApp.Models.ApplicaitonUser>.UserManager(Microsoft.AspNet.Identity.IUserStorebase(store)
.
notice I just added , string
, but it then puts the error "The best overloaded method match for "Microsoft.AspNet.Identity.UserMaager<MyApp.Models.ApplicaitonUser>.UserManager(Microsoft.AspNet.Identity.IUserStore<MyApp.Models.ApplicationUser>)' has some invalid arguments" on base(store)
.
更新 2:我改变了这个:
UPDATE 2: I changed this:
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.Create(用户,密码)抛出 EntityValidationError 说 Id 是必需的?' 字符串
:UserManager
Notice the ' string
in public class ApplicationUserManager : UserManager<ApplicationUser, string>
. But now, guess what? You guessed it--back to this issue: UserManager.Create(user, password) thowing EntityValidationError saying Id is required?
我错过了什么?
推荐答案
试试这个方法.我有同样的问题,你需要提供身份证.
Try this way. I had the same issue, you need to provide the id.
//
// 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 自定义用户和自定义角色应该很简单;我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!