Ejemplo n.º 1
0
 public SecurityStampValidator(IDbContextScopeFactory dbContextScopeFactory, IUserRepository userRepository, IClaimsIdentityFactory claimsIdentityFactory, IIdentityProvider identityProvider)
 {
     this.dbContextScopeFactory = dbContextScopeFactory;
     this.userRepository        = userRepository;
     this.claimsIdentityFactory = claimsIdentityFactory;
     this.identityProvider      = identityProvider;
 }
Ejemplo n.º 2
0
        public SignInManager(UserManager <TUser> userManager,
                             IHttpContextAccessor contextAccessor,
                             IClaimsIdentityFactory <TUser> claimsFactory,
                             IOptions <IdentityOptions> optionsAccessor = null,
                             ILoggerFactory loggerFactory = null)
        {
            if (userManager == null)
            {
                throw new ArgumentNullException(nameof(userManager));
            }
            if (contextAccessor == null || contextAccessor.Value == null)
            {
                throw new ArgumentNullException(nameof(contextAccessor));
            }
            if (claimsFactory == null)
            {
                throw new ArgumentNullException(nameof(claimsFactory));
            }

            UserManager   = userManager;
            Context       = contextAccessor.Value;
            ClaimsFactory = claimsFactory;
            Options       = optionsAccessor?.Options ?? new IdentityOptions();

            loggerFactory = loggerFactory ?? new LoggerFactory();
            Logger        = loggerFactory.Create(nameof(SignInManager <TUser>));
        }
Ejemplo n.º 3
0
        public void LogUserIn_creates_claims_identity_using_factory([Frozen] IClaimsIdentityFactory identityFactory,
                                                                    UserLoginLogoutService sut,
                                                                    string username,
                                                                    IIdentity identity)
        {
            // Act
            sut.LogUserIn(username, identity);

            // Assert
            Mock.Get(identityFactory)
            .Verify(x => x.GetIdentity(username, identity, It.IsAny <string>()), Times.Once());
        }
Ejemplo n.º 4
0
        public void LogUserIn_creates_claims_identity_with_app_cookie_login_type([Frozen] IAuthenticationManager authManager,
                                                                                 [Frozen] IClaimsIdentityFactory identityFactory,
                                                                                 UserLoginLogoutService sut,
                                                                                 string username,
                                                                                 IIdentity identity)
        {
            // Act
            sut.LogUserIn(username, identity);

            // Assert
            Mock.Get(identityFactory)
            .Verify(x => x.GetIdentity(It.IsAny <string>(), It.IsAny <IIdentity>(), "ApplicationCookie"), Times.Once());
        }
Ejemplo n.º 5
0
        public UserLoginLogoutService(IAuthenticationManager authenticationManager,
                                      IClaimsIdentityFactory claimsIdentityFactory)
        {
            if (claimsIdentityFactory == null)
            {
                throw new ArgumentNullException(nameof(claimsIdentityFactory));
            }
            if (authenticationManager == null)
            {
                throw new ArgumentNullException(nameof(authenticationManager));
            }

            this.claimsIdentityFactory = claimsIdentityFactory;
            this.authenticationManager = authenticationManager;
        }
Ejemplo n.º 6
0
        public void LogUserIn_passes_claims_identity_to_auth_manager([Frozen] IAuthenticationManager authManager,
                                                                     [Frozen] IClaimsIdentityFactory identityFactory,
                                                                     UserLoginLogoutService sut,
                                                                     string username,
                                                                     IIdentity identity,
                                                                     [NoAutoProperties] ClaimsIdentity claimsIdentity)
        {
            // Arrange
            Mock.Get(identityFactory)
            .Setup(x => x.GetIdentity(It.IsAny <string>(), It.IsAny <IIdentity>(), It.IsAny <string>()))
            .Returns(claimsIdentity);

            // Act
            sut.LogUserIn(username, identity);

            // Assert
            Mock.Get(authManager)
            .Verify(x => x.SignIn(It.IsAny <AuthenticationProperties>(), claimsIdentity), Times.Once());
        }
Ejemplo n.º 7
0
 public SignInManager(UserManager <TUser> userManager, IAuthenticationManager authenticationManager,
                      IClaimsIdentityFactory <TUser> claimsFactory)
 {
     if (userManager == null)
     {
         throw new ArgumentNullException("userManager");
     }
     if (authenticationManager == null)
     {
         throw new ArgumentNullException("authenticationManager");
     }
     if (claimsFactory == null)
     {
         throw new ArgumentNullException("claimsFactory");
     }
     UserManager           = userManager;
     AuthenticationManager = authenticationManager;
     ClaimsFactory         = claimsFactory;
 }
 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;
 }
Ejemplo n.º 9
0
        public OAuthAuthorizationChecker(IPasswordAuthenticationService authService,
                                         LoginRequestCreator loginRequestCreator,
                                         IClaimsIdentityFactory claimsIdentityFactory)
        {
            if (claimsIdentityFactory == null)
            {
                throw new ArgumentNullException(nameof(claimsIdentityFactory));
            }
            if (loginRequestCreator == null)
            {
                throw new ArgumentNullException(nameof(loginRequestCreator));
            }
            if (authService == null)
            {
                throw new ArgumentNullException(nameof(authService));
            }

            this.authService           = authService;
            this.loginRequestCreator   = loginRequestCreator;
            this.claimsIdentityFactory = claimsIdentityFactory;
        }
Ejemplo n.º 10
0
 public SignInManager(UserManager <TUser> userManager, IAuthenticationManager authenticationManager,
                      IClaimsIdentityFactory <TUser> claimsFactory, IOptionsAccessor <IdentityOptions> optionsAccessor)
 {
     if (userManager == null)
     {
         throw new ArgumentNullException("userManager");
     }
     if (authenticationManager == null)
     {
         throw new ArgumentNullException("authenticationManager");
     }
     if (claimsFactory == null)
     {
         throw new ArgumentNullException("claimsFactory");
     }
     if (optionsAccessor == null || optionsAccessor.Options == null)
     {
         throw new ArgumentNullException("optionsAccessor");
     }
     UserManager           = userManager;
     AuthenticationManager = authenticationManager;
     ClaimsFactory         = claimsFactory;
     Options = optionsAccessor.Options;
 }
Ejemplo n.º 11
0
 public SignInManager(UserManager <TUser> userManager, IContextAccessor <HttpContext> contextAccessor,
                      IClaimsIdentityFactory <TUser> claimsFactory, IOptions <IdentityOptions> optionsAccessor)
 {
     if (userManager == null)
     {
         throw new ArgumentNullException(nameof(userManager));
     }
     if (contextAccessor == null || contextAccessor.Value == null)
     {
         throw new ArgumentNullException(nameof(contextAccessor));
     }
     if (claimsFactory == null)
     {
         throw new ArgumentNullException(nameof(claimsFactory));
     }
     if (optionsAccessor == null || optionsAccessor.Options == null)
     {
         throw new ArgumentNullException(nameof(optionsAccessor));
     }
     UserManager   = userManager;
     Context       = contextAccessor.Value;
     ClaimsFactory = claimsFactory;
     Options       = optionsAccessor.Options;
 }
Ejemplo n.º 12
0
 public UserManager(IUserStore <User, int> store, IClaimsIdentityFactory <User, int> claimsIdentityFactory)
     : base(store)
 {
     ClaimsIdentityFactory = claimsIdentityFactory;
 }
Ejemplo n.º 13
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 SecurityStampValidator(IUserRepository userRepository, IClaimsIdentityFactory claimsIdentityFactory)
 {
     this.userRepository = userRepository;
     this.claimsIdentityFactory = claimsIdentityFactory;
 }
        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;
        }