在控制器操作中调用了以下内容:
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
// ...
}
}我在这里错过了什么?
发布于 2015-10-11 12:50:25
这是一个不能令人满意的办法。
卸载所有与Owin和Identity相关的Nuget包,注释掉所有未构建的内容,然后重新启动Visual,清理和重新构建,然后重新安装软件包。
当它们被安装时,由于某些原因,它们不是最新的,所以您需要返回并升级它们。
现在,再取消所有的评论,重建。突然,起作用了!
不是一个好的解决办法。
https://stackoverflow.com/questions/33064739
复制相似问题