【发布时间】:2015-10-11 12:03:07
【问题描述】:
我在控制器操作中调用了以下内容:
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
// ...
}
}
我在这里错过了什么?
【问题讨论】:
标签: asp.net-mvc asp.net-identity owin asp.net-identity-2