2015-12-21 39 views
1

我已經更新身份在我的項目2個版本,並在AccountControllerForgotPassword動作我在這一行收到No IUserTokenProvider is registered error沒有IUserTokenProvider登記錯誤更新身份後1〜2

string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); 

然後我實現IUserTokenProvider接口基礎在thisIdentityConfig我使用:

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
{ 
    var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
    //other code 
    manager.UserTokenProvider = new MyUserTokenProvider<ApplicationUser>();   
    return manager; 
} 

,但同樣的錯誤再次發生。 然後我初始化manager.UserTokenProviderForgotPassword行動和每件事情都很好。

public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindByEmailAsync(model.Email); 
       if (user == null) 
       { 
        return View("error message"); 
       } 

       UserManager.UserTokenProvider = new MyUserTokenProvider<ApplicationUser>(); 

       string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); 
       var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
       await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>"); 
       return RedirectToAction("ForgotPasswordConfirmation", "Account"); 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

我不知道是什麼問題。 任何人都可以幫助我嗎? 感謝先進。

回答

1

我找到了解決方案。每一件事情都是在identityconfig中真正實現的。問題出在AccountController。現在

public AccountController() 
     { 
     } 
public AccountController(ApplicationUserManager userManager) 
{ 
    UserManager = userManager; 
} 

public ApplicationUserManager UserManager 
{ 
    get 
    { 
     return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
    } 
    private set 
    { 
     _userManager = value; 
    } 
} 

這項工作很好:我犯了的UserManager一個新的對象,並沒有使用注射對象的userManager

在新版本
public AccountController() 
     : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) 
        { 
    _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); 
        } 

    public AccountController(UserManager<ApplicationUser> userManager) 
       { 
        UserManager = userManager; 
       } 

       public UserManager<ApplicationUser> UserManager { get; private set; } 

+0

MyUserTokenProvider的外觀如何? – Ovis

相關問題