Пример #1
0
        public static async Task <UserWithClaims> CreateAsync(this UserManager <IdentityUser> userManager, IUserFactory factory, UserValues values)
        {
            var user = factory.Create(values.Email);

            try
            {
                await DoChecked(() => userManager.CreateAsync(user), "Cannot create user.");

                var claims = values.ToClaims(true);

                if (claims.Count > 0)
                {
                    await DoChecked(() => userManager.AddClaimsAsync(user, claims), "Cannot add user.");
                }

                if (!string.IsNullOrWhiteSpace(values.Password))
                {
                    await DoChecked(() => userManager.AddPasswordAsync(user, values.Password), "Cannot create user.");
                }
            }
            catch
            {
                await userManager.DeleteAsync(user);

                throw;
            }

            return((await userManager.ResolveUserAsync(user)) !);
        }
Пример #2
0
        public static async Task UpdateAsync(this UserManager <IdentityUser> userManager, IdentityUser user, UserValues values)
        {
            if (user == null)
            {
                throw new DomainObjectNotFoundException("Id", typeof(IdentityUser));
            }

            if (!string.IsNullOrWhiteSpace(values.Email) && values.Email != user.Email)
            {
                await DoChecked(() => userManager.SetEmailAsync(user, values.Email), "Cannot update email.");
                await DoChecked(() => userManager.SetUserNameAsync(user, values.Email), "Cannot update email.");
            }

            await DoChecked(() => userManager.SyncClaimsAsync(user, values.ToClaims(false)), "Cannot update user.");

            if (!string.IsNullOrWhiteSpace(values.Password))
            {
                await DoChecked(() => userManager.RemovePasswordAsync(user), "Cannot replace password.");
                await DoChecked(() => userManager.AddPasswordAsync(user, values.Password), "Cannot replace password.");
            }
        }