예제 #1
0
 private void AddModelErrors(IdentityResult identityResult)
 {
     foreach (var error in identityResult.Errors)
     {
         this.ModelState.AddModelError(string.Empty, error);
     }
 }
        public virtual async Task<IdentityResult> ResetAccessFailedAttemptsAsync(string userId)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (user.AccessFailedCount == 0)
            {
                identityResult = new IdentityResult(true, null);
            }
            else
            {
                user.AccessFailedCount = 0;
                identityResult = await this.UpdateAsync(user);
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> SetLockoutEndDateAsync(string userId, DateTimeOffset lockoutEndDate)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (!user.LockoutEnabled)
            {
                identityResult = new IdentityResult(false, new[] { Resource.LockoutNotEnabled });
            }
            else
            {
                user.LockoutEndDateUtc = lockoutEndDate == DateTimeOffset.MinValue ? new DateTime?() : lockoutEndDate.UtcDateTime;
                identityResult = await this.UpdateAsync(user);
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> UpdatePhoneNumberAsync(string userId, string phoneNumber, string token)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (await this.ValidateUserTokenAsync(userId, phoneNumber, token))
            {
                user.PhoneNumber = phoneNumber;
                user.PhoneNumberConfirmed = true;
                user.SecurityStamp = this.CreateSecurityStamp();

                identityResult = await this.UpdateAsync(user);
            }
            else
            {
                identityResult = new IdentityResult(false, new[] { Resource.InvalidToken });
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> ConfirmEmailAsync(string userId, string token)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (await this.ValidateUserTokenAsync(userId, EmailConfirmationUserTokenModifier, token))
            {
                user.EmailConfirmed = true;
                identityResult = await this.UpdateAsync(user);
            }
            else
            {
                identityResult = new IdentityResult(false, new[] { Resource.InvalidToken });
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> RemoveRolesAsync(string userId, params string[] roles)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;

            var userRoles = await this.userRepository.GetRolesAsync(user);
            if (!roles.All(userRole => userRoles.Contains(userRole)))
            {
                identityResult = new IdentityResult(false, new[] { Resource.UserNotInRole });
            }
            else
            {
                foreach (var userRole in roles)
                {
                    await this.userRepository.RemoveFromRoleAsync(user, userRole);
                }

                identityResult = await this.UpdateAsync(user);
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> RemoveRoleAsync(string userId, string role)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (!await this.userRepository.IsInRoleAsync(user, role))
            {
                identityResult = new IdentityResult(false, new[] { Resource.UserNotInRole });
            }
            else
            {
                await this.userRepository.RemoveFromRoleAsync(user, role);
                identityResult = await this.UpdateAsync(user);
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> AddRoleAsync(string userId, string role)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;

            var userRoles = await this.userRepository.GetRolesAsync(user);
            if (userRoles.Contains(role))
            {
                identityResult = new IdentityResult(false, new[] { Resource.UserAlreadyInRole });
            }
            else
            {
                await this.userRepository.AddToRoleAsync(user, role);
                identityResult = await this.UpdateAsync(user);
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> CreateLoginAsync(string userId, UserLinkedLogin userLinkedLogin)
        {
            User userFoundById = await this.FindByIdAsync(userId);
            if (userFoundById == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;

            User userFoundByLogin = await this.FindByLoginAsync(userLinkedLogin);
            if (userFoundByLogin != null)
            {
                identityResult = new IdentityResult(false, new[] { Resource.ExternalLoginExists });
            }
            else
            {
                await this.userRepository.AddLoginAsync(userFoundById, userLinkedLogin);
                identityResult = await this.UpdateAsync(userFoundById);
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> ResetPasswordAsync(string userId, string userToken, string newPassword)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (await this.ValidateUserTokenAsync(userId, ResetPasswordUserTokenModifier, userToken))
            {
                identityResult = await this.UpdatePasswordAsync(user, newPassword);
                if (identityResult.IsValid)
                {
                    identityResult = await this.UpdateAsync(user);
                }
            }
            else
            {
                identityResult = new IdentityResult(false, new[] { Resource.InvalidToken });
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> UpdatePasswordAsync(string userId, string currentPassword, string newPassword)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (this.VerifyHashedPassword(user, currentPassword))
            {
                identityResult = await this.UpdatePasswordAsync(user, newPassword);
                if (identityResult.IsValid)
                {
                    identityResult = await this.UpdateAsync(user);
                }
            }
            else
            {
                identityResult = new IdentityResult(false, new[] { Resource.PasswordMismatch });
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> SetPasswordAsync(string userId, string password)
        {
            User user = await this.FindByIdAsync(userId);
            if (user == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.UserIdNotFound, userId));
            }

            IdentityResult identityResult;
            if (string.IsNullOrEmpty(user.PasswordHash))
            {
                identityResult = await this.UpdatePasswordAsync(user, password);
                if (identityResult.IsValid)
                {
                    identityResult = await this.UpdateAsync(user);
                }
            }
            else
            {
                identityResult = new IdentityResult(false, new[] { Resource.UserAlreadyHasPassword });
            }

            return identityResult;
        }
        public virtual async Task<IdentityResult> DeleteAsync(User user)
        {
            await this.userRepository.DeleteAsync(user);

            IdentityResult identityResult = new IdentityResult(true, null);

            return identityResult;
        }