Sea*_*ome 4 asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5 asp.net-identity
使用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)]
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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新: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 };
}
}
Run Code Online (Sandbox Code Playgroud)
对此:
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 };
}
}
Run Code Online (Sandbox Code Playgroud)
注意我刚刚添加, string,但它然后将错误"最佳重载方法匹配"Microsoft.AspNet.Identity.UserMaager.UserManager(Microsoft.AspNet.Identity.IUserStore)'有一些无效的参数"on base(store).
更新2:我改变了这个:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
...
}
Run Code Online (Sandbox Code Playgroud)
对此:
public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
...
}
Run Code Online (Sandbox Code Playgroud)
注意' string在public class ApplicationUserManager : UserManager<ApplicationUser, string>.但现在,猜猜怎么着?你猜对了 - 回到这个问题:UserManager.Create(用户,密码)thowing EntityValidationError说Id是必需的?
我错过了什么?
试试这种方式.我有同样的问题,你需要提供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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8622 次 |
| 最近记录: |