public UserIdentityApplicationService(
     IClaimsIdentityFactory claimsIdentityFactory,
     IPasswordHasher passwordHasher,
     IIdentityValidator<string> passwordIdentityValidator,
     IIdentityValidator<User> userIdentityValidator,
     IUserRepository userRepository,
     IDbContextScopeFactory dbContextScopeFactory,
     IUserTokenProvider userTokenProvider,
     IUserTokenTwoFactorProvider userTokenTwoFactorProvider,
     IMapper mapper,
     ILog logger)
     : base(dbContextScopeFactory, mapper, logger)
 {
     this.claimsIdentityFactory = claimsIdentityFactory;
     this.passwordHasher = passwordHasher;
     this.passwordIdentityValidator = passwordIdentityValidator;
     this.userIdentityValidator = userIdentityValidator;
     this.userRepository = userRepository;
     this.userTokenProvider = userTokenProvider;
     this.userTokenTwoFactorProvider = userTokenTwoFactorProvider;
 }
Esempio n. 2
0
        private Container ConfigureIoCContainer(IAppBuilder appBuilder)
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

            appBuilder.Use(
                async(context, next) =>
            {
                using (container.BeginExecutionContextScope())
                {
                    await next();
                }
            });

            container.Register <ILog>(() => LogManager.GetLogger(typeof(LogManager)));

            container.RegisterCollection <Profile>();
            container.Register <IMapper>(() =>
            {
                Mapper.Initialize(configuration =>
                {
                    var profiles = container.GetAllInstances <Profile>();

                    foreach (var profile in profiles)
                    {
                        configuration.AddProfile(profile);
                    }
                });

                return(Mapper.Instance);
            }, Lifestyle.Singleton);

            container.Register <IAmbientDbContextLocator, AmbientDbContextLocator>(Lifestyle.Scoped);
            container.Register <IDbContextScopeFactory>(() => new DbContextScopeFactory());
            container.Register <IUserRepository, UserRepository <ApplicationDbContext> >(Lifestyle.Scoped);

            container.Register <IClaimsIdentityFactory, ClaimsIdentityFactory>(Lifestyle.Scoped);
            container.Register <IPasswordHasher, CryptoPasswordHasher>(Lifestyle.Scoped);
            container.Register <IIdentityValidator <string> >(
                () =>
            {
                var passwordValidator = new PasswordValidator
                {
                    RequiredLength          = 6,
                    RequireNonLetterOrDigit = true,
                    RequireDigit            = true,
                    RequireLowercase        = true,
                    RequireUppercase        = true,
                };

                return(passwordValidator);
            });
            container.Register <IIdentityValidator <User> >(
                () =>
            {
                var userRepository = container.GetInstance <IUserRepository>();
                var userValidator  = new UserValidator(userRepository)
                {
                    AllowOnlyAlphanumericUserNames = false,
                    RequireUniqueEmail             = true
                };

                return(userValidator);
            });
            container.Register <IUserTokenProvider>(
                () =>
            {
                var dataProtectionProvider     = appBuilder.GetDataProtectionProvider();
                var dataProtector              = dataProtectionProvider.Create("ASP.NET Identity");
                var dataProtectorTokenProvider = new DataProtectorTokenProvider(dataProtector);

                return(dataProtectorTokenProvider);
            });

            container.Register <IAuthenticationService, Rfc6238AuthenticationService>(Lifestyle.Scoped);
            container.Register <IAuthenticationManager>(
                () =>
            {
                if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null &&
                    container.IsVerifying)
                {
                    return(new OwinContext().Authentication);
                }

                return(HttpContext.Current.GetOwinContext().Authentication);
            });
            container.Register <IIdentityProvider, IdentityProvider>(Lifestyle.Scoped);
            container.Register <IIdentityMessageService, SmsService>(Lifestyle.Scoped);
            container.Register <IUserTokenTwoFactorProvider, UserTokenTwoFactorProvider>(Lifestyle.Scoped);

            container.Register <IUserIdentityApplicationService>(
                () =>
            {
                IAuthenticationManager authenticationManager = container.GetInstance <IAuthenticationManager>();
                IClaimsIdentityFactory claimsIdentityFactory = container.GetInstance <IClaimsIdentityFactory>();
                IPasswordHasher passwordHasher = container.GetInstance <IPasswordHasher>();
                IIdentityValidator <string> passwordIdentityValidator = container.GetInstance <IIdentityValidator <string> >();
                IIdentityValidator <User> userIdentityValidator       = container.GetInstance <IIdentityValidator <User> >();
                IUserRepository userRepository         = container.GetInstance <IUserRepository>();
                IUserTokenProvider userTokenProvider   = container.GetInstance <IUserTokenProvider>();
                IIdentityProvider userIdentityProvider = container.GetInstance <IIdentityProvider>();
                IUserTokenTwoFactorProvider userTokenTwoFactorProvider = container.GetInstance <IUserTokenTwoFactorProvider>();
                IDbContextScopeFactory dbContextScopeFactory           = container.GetInstance <IDbContextScopeFactory>();
                IMapper mapper = container.GetInstance <IMapper>();
                ILog logger    = container.GetInstance <ILog>();

                var userIdentityApplicationService = new UserIdentityApplicationService(
                    authenticationManager,
                    claimsIdentityFactory,
                    passwordHasher,
                    passwordIdentityValidator,
                    userIdentityValidator,
                    userRepository,
                    userTokenProvider,
                    userIdentityProvider,
                    userTokenTwoFactorProvider,
                    dbContextScopeFactory,
                    mapper,
                    logger);

                var authenticationService = container.GetInstance <IAuthenticationService>();
                userIdentityApplicationService.RegisterTwoFactorAuthenticationProvider("Phone Code", new PhoneNumberTokenProvider(authenticationService, new SmsService()));
                userIdentityApplicationService.RegisterTwoFactorAuthenticationProvider("Email Code", new EmailTokenProvider(authenticationService, new EmailService()));

                return(userIdentityApplicationService);
            });

            container.Register <SecurityStampValidator>(
                () =>
            {
                IDbContextScopeFactory dbContextScopeFactory = container.GetInstance <IDbContextScopeFactory>();
                IClaimsIdentityFactory claimsIdentityFactory = container.GetInstance <IClaimsIdentityFactory>();
                IUserRepository userRepository     = container.GetInstance <IUserRepository>();
                IIdentityProvider identityProvider = container.GetInstance <IIdentityProvider>();

                var securityStampValidator = new SecurityStampValidator(dbContextScopeFactory, userRepository, claimsIdentityFactory, identityProvider);

                return(securityStampValidator);
            });

            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.Verify();

            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorDependencyResolver(container);

            return(container);
        }
        public UserIdentityApplicationService(
            IAuthenticationManager authenticationManager,
            IClaimsIdentityFactory claimsIdentityFactory,
            IPasswordHasher passwordHasher,
            IIdentityValidator <string> passwordIdentityValidator,
            IIdentityValidator <User> userIdentityValidator,
            IUserRepository userRepository,
            IUserTokenProvider userTokenProvider,
            IIdentityProvider userIdentityProvider,
            IUserTokenTwoFactorProvider userTokenTwoFactorProvider,
            IDbContextScopeFactory dbContextScopeFactory,
            IMapper mapper,
            ILog logger)
            : base(dbContextScopeFactory, mapper, logger)
        {
            this.authenticationManager      = authenticationManager;
            this.claimsIdentityFactory      = claimsIdentityFactory;
            this.passwordHasher             = passwordHasher;
            this.passwordIdentityValidator  = passwordIdentityValidator;
            this.userIdentityValidator      = userIdentityValidator;
            this.userRepository             = userRepository;
            this.userTokenProvider          = userTokenProvider;
            this.userIdentityProvider       = userIdentityProvider;
            this.userTokenTwoFactorProvider = userTokenTwoFactorProvider;

            this.AuthenticationType                   = Infrastructure.Identity.Model.AuthenticationType.ApplicationCookie;
            this.UserLockoutEnabledByDefault          = true;
            this.DefaultAccountLockoutTimeSpan        = TimeSpan.FromMinutes(DefaultAccountLockoutTimeSpanInMinutes);
            this.MaxFailedAccessAttemptsBeforeLockout = DefaultMaxFailedAccessAttemptsBeforeLockout;
        }