Exemplo n.º 1
0
        public static IlevusUserManager Create(
            IdentityFactoryOptions <IlevusUserManager> options,
            IOwinContext context)
        {
            var manager = new IlevusUserManager(new IlevusUserStore(context.Get <IlevusIdentityContext>()));

            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator <IlevusUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail             = true
            };
            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength          = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit            = true,
                RequireLowercase        = true,
                RequireUppercase        = false
            };

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault          = true;
            manager.DefaultAccountLockoutTimeSpan        = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            manager.EmailService = new IlevusEmailService();

            var dataProtectionProvider = options.DataProtectionProvider;

            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider <IlevusUser>(
                        dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return(manager);
        }
Exemplo n.º 2
0
        //Create [email protected] with password=Admin@12345 in the Admin role
        public static void InitializeIdentity(IlevusIdentityContext db)
        {
            var userManager = new IlevusUserManager(new IlevusUserStore(db));
            var roleManager = new IlevusRoleManager(new IlevusRoleStore(db));

            const string email    = "*****@*****.**";
            const string password = "******";

            const string roleName = "SysAdmin";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                role = new IlevusRole(roleName)
                {
                    Description = "Todas as permissões do sistema."
                };
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(email);

            if (user == null)
            {
                user = new IlevusUser
                {
                    UserName       = email,
                    Email          = email,
                    EmailConfirmed = true,
                    Name           = "Administrador",
                    Surname        = "do Sistema",
                    Sex            = "M",
                    PhoneNumber    = "031 999999999",
                    Image          = null,

                    Professional = new UserProfessionalEntity()
                    {
                        City    = "Belo Horizonte",
                        County  = "Minas Gerais",
                        Country = "Brasil",
                        Address = "Sede da Ilevus",
                    }
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForUser = userManager.GetRoles(user.Id);

            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }


            const string userEmail    = "*****@*****.**";
            const string userPassword = "******";

            const string userRoleName = "User";

            //Create Role Admin if it does not exist
            var userRole = roleManager.FindByName(userRoleName);

            if (userRole == null)
            {
                userRole = new IlevusRole(userRoleName)
                {
                    Description = "Usuário comum do sistema."
                };
                roleManager.Create(userRole);
            }

            var commonUser = userManager.FindByName(userEmail);

            if (commonUser == null)
            {
                commonUser = new IlevusUser
                {
                    UserName       = userEmail,
                    Email          = userEmail,
                    EmailConfirmed = true,
                    Name           = "Usuário",
                    Surname        = "Exemplo",
                    Sex            = "M",
                    PhoneNumber    = "031 988888888",
                    Image          = null,

                    Professional = new UserProfessionalEntity()
                    {
                        City    = "Belo Horizonte",
                        County  = "Minas Gerais",
                        Country = "Brasil",
                        Address = "Sede da Ilevus",
                    }
                };
                var result = userManager.Create(commonUser, userPassword);
                result = userManager.SetLockoutEnabled(commonUser.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForCommonUser = userManager.GetRoles(commonUser.Id);

            if (!rolesForCommonUser.Contains(userRole.Name))
            {
                var result = userManager.AddToRole(commonUser.Id, userRole.Name);
            }
        }