я следующее вызывается в действии контроллера:Owin не возвращаются 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
// ...
}
}
Что мне здесь не хватает?
К сожалению, это решение действительно помогло мне. –