Owin 不返回 UserManager
Owin not returning 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
方法被调用但没有 return 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,清理并重建,然后再次重新安装这些包。
安装后,由于某些原因它们不是最新的,因此您需要返回并全部升级。
现在再次取消注释并重建。突然间,它起作用了!
不是一个好的解决方案。
我在控制器操作中调用了以下内容:
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
方法被调用但没有 return 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,清理并重建,然后再次重新安装这些包。
安装后,由于某些原因它们不是最新的,因此您需要返回并全部升级。
现在再次取消注释并重建。突然间,它起作用了!
不是一个好的解决方案。