Exemplo n.º 1
0
        internal static async Task UpdateUser(RivingtonContext context, UserManager <ApplicationUser> userManager, UserForSeeder userForSeeder)
        {
            var existingUser = await userManager.FindByNameAsync(userForSeeder.AppUser.UserName);

            if (existingUser == null)
            {
                await CreateUser(userManager, userForSeeder);
            }
            else
            {
                existingUser.LastModifiedDate = DateTime.Now;
                existingUser.UserName         = userForSeeder.AppUser.UserName;
                existingUser.Email            = userForSeeder.AppUser.Email;
                existingUser.EmailConfirmed   = userForSeeder.AppUser.EmailConfirmed;
                existingUser.LockoutEnabled   = userForSeeder.AppUser.LockoutEnabled;
                existingUser.PasswordHash     = userForSeeder.AppUser.PasswordHash;

                var existingRoles = await userManager.GetRolesAsync(existingUser);

                await userManager.RemoveFromRolesAsync(existingUser, existingRoles);

                foreach (var role in userForSeeder.Roles)
                {
                    await userManager.AddToRoleAsync(existingUser, role);
                }

                await userManager.UpdateAsync(existingUser);
            }

            await context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        internal static async Task CreateUser(UserManager <ApplicationUser> userManager, UserForSeeder userForSeeder)
        {
            if (await userManager.FindByNameAsync(userForSeeder.AppUser.UserName) != null)
            {
                return;
            }

            await userManager.CreateAsync(userForSeeder.AppUser, userForSeeder.Password);

            foreach (var role in userForSeeder.Roles)
            {
                await userManager.AddToRoleAsync(userForSeeder.AppUser, role);
            }
        }