没有返回UserManager



我在控制器动作中调用了以下内容:

private ApplicationUserManager ApplicationUserManager
    => HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

我的IdentityConfig看起来像:

internal class IdentityConfig
{
    public void Configuration(IAppBuilder appBuilder)
    {
        appBuilder.CreatePerOwinContext(ApplicationIdentityDbContext.Create);
        appBuilder.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        appBuilder.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
        var cookieAuthenticationOptions = new CookieAuthenticationOptions
                                          {
                                              AuthenticationType =
                                                  DefaultAuthenticationTypes
                                                  .ApplicationCookie,
                                              LoginPath =
                                                  new PathString("/Home/Login")
                                          };
        appBuilder.UseCookieAuthentication(cookieAuthenticationOptions);
    }
}

我可以逐步执行并看到配置被调用,并且Create方法被调用并且不返回null

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
                                                IOwinContext context)
{
    var db = context.Get<ApplicationIdentityDbContext>();
    var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(db));
    manager.PasswordValidator = new PasswordValidator();
    return manager;
}

当我得到ApplicationUserManager虽然,它是空的。

public class AccountController : Controller
{
    private ApplicationUserManager ApplicationUserManager
        => HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        var user = await ApplicationUserManager.FindAsync(model.Username, model.Password);  // <- Null reference exception
        // ...
    }
}

我在这里错过了什么?

修复不令人满意。

卸载所有与Owin和Identity相关的Nuget包,注释掉所有不能构建的包,然后重新启动Visual Studio,清理和重建,然后重新安装包。

当它们被安装时,由于某些原因它们不是最新的,所以你需要返回并升级它们。

现在再次取消注释并重新构建。突然,它起作用了!

不是个好办法

相关内容

  • 没有找到相关文章

最新更新