예제 #1
0
        private void addUsers(TaskMDbContext dbContext)
        {
            string          email = "*****@*****.**";
            ApplicationUser user  = dbContext.Users.FirstOrDefault(x => x.Email == email);

            if (user == null)
            {
                IdentityRole   identityRole = dbContext.Roles.First(x => x.Name == "Admin");
                var            userId       = Guid.NewGuid().ToString();
                PasswordHasher hasher       = new PasswordHasher();
                string         pass         = hasher.HashPassword("Pass123");

                IdentityUserRole userRole = new IdentityUserRole()
                {
                    UserId = userId, RoleId = identityRole.Id
                };
                user = new ApplicationUser()
                {
                    Email        = email,
                    UserName     = email,
                    Id           = userId,
                    Roles        = { userRole },
                    PasswordHash = pass
                };
                dbContext.Users.Add(user);
                dbContext.SaveChanges();
            }
        }
예제 #2
0
        public static ApplicationUserManager Create(IdentityFactoryOptions <ApplicationUserManager> options, IOwinContext context)
        {
            TaskMDbContext dbContext             = context.Get <TaskMDbContext>();
            IUserStore <ApplicationUser> store   = new UserStore <ApplicationUser>();
            ApplicationUserManager       manager = new ApplicationUserManager(store);

            manager.UserValidator = new UserValidator <ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = true,
                RequireUniqueEmail             = true
            };
            manager.PasswordValidator = new PasswordValidator()
            {
                RequiredLength = 6,
            };

            return(manager);
        }
예제 #3
0
        private void addRoles(TaskMDbContext dbContext)
        {
            List <string> roles = new List <string>()
            {
                "Admin", "User"
            };

            foreach (string r in roles)
            {
                IdentityRole identityRole = dbContext.Roles.FirstOrDefault(x => x.Name == r);
                if (identityRole == null)
                {
                    identityRole = new IdentityRole(r);
                    dbContext.Roles.Add(identityRole);
                }
            }

            dbContext.SaveChanges();
        }