E
E
eRKa2016-12-10 23:14:32
ASP.NET
eRKa, 2016-12-10 23:14:32

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.
The order is this, everything is standard:
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;
        }
    }

In Startup.cs
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions());

Well then I try to get my manager
Request.GetOwinContext().GetUserManager<ApplicationUserManager>()

Just in case, I tried
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()

Does not work.
Half a day already sat on this garbage. What did I miss? What are the options?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2016-12-12
@kttotto

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.

A
Andrew, 2016-12-11
@impwx

The person had a similar problem , and this line helped:

app.UseCookieAuthentication(new CookieAuthenticationOptions());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question