3

使用http://www.asp.net/identity的例子我已經得到了這麼多。 RoleManager完美地工作,我把UserManager相同。我認爲一切正確,但我似乎無法正確控制一個UserManager。哪裏不對?在一個點上,我成功地獲得UserManager工作,但得到了一個EntityValidationError說:「需要ID」創建與UserManager.Create(user, password);新用戶時,張貼在這個問題UserManager.Create(user, password) thowing EntityValidationError saying Id is required?AspNet.Identity自定義用戶和自定義角色應該很簡單;我錯過了什麼?

碰運氣的一段時間後,所以,我已創建類似於以下的所有內容,但在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,但它會在base(store)上出現錯誤「最佳重載方法匹配」,Microsoft.AspNet.Identity.UserMaager.UserManager(Microsoft.AspNet.Identity.IUserStore)'有一些無效參數「。

更新2:在public class ApplicationUserManager : UserManager<ApplicationUser, string>

public class ApplicationUserManager : UserManager<ApplicationUser, string> 
    { 
     public ApplicationUserManager(IUserStore<ApplicationUser, string> store) 
     ... 
    } 

通知的' string:我改變這樣的:

public class ApplicationUserManager : UserManager<ApplicationUser> 
    { 
     public ApplicationUserManager(IUserStore<ApplicationUser, string> store) 
     ... 
    } 

此。但現在,猜猜怎麼樣?你猜對了 - 回到這個問題:UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

我在想什麼?

+0

可能重複[UserManager.Create(用戶名,密碼)異常觸發EntityValidationError說標識要求?(http://stackoverflow.com/questions/22698744/usermanager-createuser-password-thowing-entityvalidationerror-說-ID-是-RE) –

回答

6

試試這個方法。我有同樣的問題,你需要提供的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); 
    } 
相關問題