Example #1
0
        public IdentityServices(DbContext context, IAuthenticationManager authenticationMana, UserStore<IdentityUser> userStor, UserManager<IdentityUser> userMena)
        {
            db = context;
            userStore = userStor;
            userMenager = userMena;
            authenticationManager = authenticationMana;

            userMenager.UserValidator = new UserValidator<IdentityUser>(userMenager) { RequireUniqueEmail = true, AllowOnlyAlphanumericUserNames = false };
            userMenager.PasswordValidator = new PasswordValidator() { RequiredLength = 6, RequireLowercase = true, RequireUppercase = true, RequireDigit = true };

            signInMenager = new SignInManager<IdentityUser, string>(userMenager, authenticationManager);
            
            userMenager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<IdentityUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });

            userMenager.EmailService = new EmailService();

            var dataProtectionProvider = Startup.dataProtectionProvider;

            if (dataProtectionProvider != null)
            {
                IDataProtector dataProtector = dataProtectionProvider.Create("ASP.NET Identity");
                userMenager.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(dataProtector);
            }
        }
 public static UserManager<ApplicationIdentityUser, int> CreateUserManager(DbContext context)
 {
     var manager = new UserManager<ApplicationIdentityUser, int>(new UserStore<ApplicationIdentityUser, ApplicationIdentityRole, int, ApplicationIdentityUserLogin, ApplicationIdentityUserRole, ApplicationIdentityUserClaim>(context));
     // Configure validation logic for usernames
     manager.UserValidator = new  UserValidator<ApplicationIdentityUser, int>(manager)
     {
         AllowOnlyAlphanumericUserNames = false,
         RequireUniqueEmail = true
     };
     // Configure validation logic for passwords
     manager.PasswordValidator = new PasswordValidator
     {
         RequiredLength = 6,
         RequireNonLetterOrDigit = true,
         RequireDigit = true,
         RequireLowercase = true,
         RequireUppercase = true,
     };
     // Configure user lockout defaults
     manager.UserLockoutEnabledByDefault = true;
     manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
     manager.MaxFailedAccessAttemptsBeforeLockout = 5;
     // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
     // You can write your own provider and plug in here.
     manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationIdentityUser, int>
     {
         MessageFormat = "Your security code is: {0}"
     });
     manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationIdentityUser, int>
     {
         Subject = "SecurityCode",
         BodyFormat = "Your security code is {0}"
     });
     manager.EmailService = new EmailService();
     manager.SmsService = new SmsService();
     return manager;
 }