UserManager construction

May 14, 2014 at 4:14 PM
Edited May 14, 2014 at 4:16 PM
As I'm trying to understand UserManager, I found that, based on code samples, the injected ApplicationUserManager's constructor runs; however, the create method is not processed. The only way to grab the actual UserManager is via Owin context. This line
__m_userManager ?HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>
doesn't make sense to me, since m_userManager is not fully built via Create method. I would think the code should be reversed HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>??m_userManager. Can someone explain if I'm configured incorrectly? Do I need same defined settings in constructor as in Create method?

Thanks

Controller:

private ApplicationUserManager m_userManager;
      
        public LoginController( ApplicationUserManager userManager )
        {
            m_userManager = userManager;
        }

 public ApplicationUserManager UserManager
        {
            get
            {
                return m_userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                m_userManager = value;
            }
        } 


Startup:

    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            
            app.CreatePerOwinContext( ApplicationDbContext.Create );

            // Configure the UserManager
            app.CreatePerOwinContext<ApplicationUserManager>( ApplicationUserManager.Create );

            // Enable the application to use a cookie to store information for the signed in user
            app.UseCookieAuthentication(options: new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Security/Login"),
            

 public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        private readonly int PASSWORD_HISTORY_LIMIT = 5;

        public ApplicationUserManager( IUserStore<ApplicationUser> store )
            : base( store )
        {           
            //MaxFailedAccessAttemptsBeforeLockout = 3;
            //UserLockoutEnabledByDefault = true;
            //DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes( 5 );
        }

        public static ApplicationUserManager Create(
                 IdentityFactoryOptions<ApplicationUserManager> options,
                 IOwinContext context
            )
        {
            var manager = new ApplicationUserManager( new UserStore<ApplicationUser>( new ApplicationDbContext() ) );
            // Configure the application user manager
            manager.UserValidator = new UserValidator<ApplicationUser>( manager )
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true

            };
            manager.PasswordValidator = new PasswordValidator()
            {
                RequiredLength = 8,
                RequireNonLetterOrDigit = true,
                RequireDigit = true
            };

            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes( 5 );
            manager.MaxFailedAccessAttemptsBeforeLockout = 3;


            // Configure the application user manager
            manager.UserValidator = new UserValidator<ApplicationUser>( manager )
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };
          
            return manager;
        }
    }