Answer the question
In order to leave comments, you need to log in
Why does GetUserManager() return null?
There is a task: asp.net selfhost, Entity, Identity, mysql, code-first.
At this stage, everything works, requests go through, the database is created, data is written, read.
But the UserManager does not want to, Request.GetOwinContext() gives a value, but GetUserManager returns null on it. Google says that in the configuration you need
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
But I had it from the very beginning. public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Настройка логики проверки имен пользователей
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Настройка логики проверки паролей
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
Request.GetOwinContext().GetUserManager<ApplicationUserManager>()
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()
Answer the question
In order to leave comments, you need to log in
Solution found.
In the Startup file, the line
should be at the very bottom, after all your settings.
And don't be embarrassed that
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
does not use the config parameter and it seems like there is no difference. In general, there is a difference and it matters.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question