コード例 #1
0
        public static async System.Threading.Tasks.Task <IdentityResult> CreateUserAsync(TaskManagementDBContext context, UserManager <User> userManager, User user, string password, string role)
        {
            if (!context.Users.Any(u => u.UserName == user.UserName))
            {
                var passwordHasher = new PasswordHasher <User>();
                var hashed         = passwordHasher.HashPassword(user, password);
                user.PasswordHash = hashed;

                var userStore = new UserStore <User>(context);
                var result    = await userStore.CreateAsync(user);

                if (result.Succeeded)
                {
                    context.SaveChangesAsync().Wait();
                }
            }

            var assignRole = await AssignRoles(userManager, user.Email, role);

            if (assignRole.Succeeded)
            {
                context.SaveChangesAsync().Wait();
            }

            return(assignRole);
        }
コード例 #2
0
 public void SaveChanges() => _context.SaveChangesAsync();