问题描述
从使用例子这一步我已经得到。在 RoleManager
完美的作品,我对待的UserManager
相同。我觉得一切都是正确的,但我似乎无法为新的一个正确的UserManager
在控制器中。哪里不对?有一次,我成功地获得的UserManager
工作,但得到了一个 EntityValidationError
说:标识要求的时候创建具有 UserManager.Create(用户名,密码)的新用户;
,张贴在这一问题UserManager.Create(user,密码)异常触发EntityValidationError说时需出示身份证?
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?
于是碰运气的一段时间后,我已经创造了一切像下面,但我对新ApplicationUserManager得到一个编译时错误(新ApplicationUserStore(新MyAppDb()))
说,为最佳重载方法匹配'MyApp.Models.ApplicationUserManager.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore)'有一些无效参数,试图在我的控制器创建的UserManager时:
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, "The best overloaded method match for 'MyApp.Models.ApplicationUserManager.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore)' has some invalid arguments" 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 };
}
}
我刚才添加的通知,串
,但随后它把错误Microsoft.AspNet.Identity.UserMaager.UserManager的最佳重载的方法匹配(Microsoft.AspNet .Identity.IUserStore)对基(存储)
。
notice I just added , string
, but it then puts the error "The best overloaded method match for "Microsoft.AspNet.Identity.UserMaager.UserManager(Microsoft.AspNet.Identity.IUserStore)' 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)
...
}
注意'字符串
在公共类ApplicationUserManager:&的UserManager LT; ApplicationUser,串&GT;
。但现在,你猜怎么着?你猜对了 - 回到这个问题:UserManager.Create(user,密码)异常触发EntityValidationError说时需出示身份证?
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?
我缺少什么?的
What am I missing?
推荐答案
试试这个方法。我有同样的问题,您需要提供身份证。
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自定义用户和自定义角色应该是简单的;我缺少什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!