Asp.net mvc Owin未返回UserManager
我在控制器操作中调用了以下命令:Asp.net mvc Owin未返回UserManager,asp.net-mvc,asp.net-identity,owin,asp.net-identity-2,Asp.net Mvc,Asp.net Identity,Owin,Asp.net Identity 2,我在控制器操作中调用了以下命令: private ApplicationUserManager ApplicationUserManager => HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 但是,当我获得ApplicationUserManager时,它是空的 public class AccountController : Controller { priva
private ApplicationUserManager ApplicationUserManager
=> HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
但是,当我获得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
// ...
}
}
公共类AccountController:控制器
{
专用应用程序服务器管理器应用程序服务器管理器
=>HttpContext.GetOwinContext().GetUserManager();
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginView模型,字符串返回URL)
{
var user=wait ApplicationUserManager.FindAsync(model.Username,model.Password);//修复程序不令人满意
卸载与Owin和Identity相关的所有Nuget软件包,注释掉所有未生成的内容,然后重新启动Visual Studio,清理并重建这些软件包,然后重新安装这些软件包
安装后,由于某些原因,它们不是最新的,因此您需要返回并升级它们
现在,再次取消对所有内容的注释并重建。突然,它开始工作了
这不是一个好的解决方案。遗憾的是,这个解决方案实际上也帮助了我。
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;
}
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
// ...
}
}