Esempio n. 1
0
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> UpdateUser(UserDTO userdata)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var findresult = await FindExistingUserAndRemapToEntity(userdata);

                if (findresult.Errors.Count() > 0)
                {
                    return(false, findresult.Errors);
                }

                var updateresult = await _userManager.UpdateAsync(findresult.User);

                if (!updateresult.Succeeded)
                {
                    return(false, updateresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                if (userdata.HasChangeEmail)
                {
                    var confirmmailresult = await _useremailmanager.GenerateEmailConfirmation(findresult.User.Id);

                    if (!confirmmailresult.Succeeded)
                    {
                        return(false, new List <FieldValidationErrorDTO>()
                        {
                            new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(confirmmailresult.Error.Key), confirmmailresult.Error.Description)
                        });
                    }
                }

                scope.Complete();
                return(true, new List <FieldValidationErrorDTO>());
            }
        }
Esempio n. 2
0
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> CreateStandardUser(UserDTO userdata)
        {
            // TODO: hardcoded standard user role name!
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                var userresult = CreateUserAndMapToEntity(userdata);
                if (userresult.Errors.Count() > 0)
                {
                    return(false, userresult.Errors);
                }

                var createresult = await _userManager.CreateAsync(userresult.User, userdata.PasswordClear);

                if (!createresult.Succeeded)
                {
                    return(false, createresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                userresult.User.RegisteredTimestamp = DateTime.UtcNow;
                var updateresult = await _userManager.UpdateAsync(userresult.User);

                if (!updateresult.Succeeded)
                {
                    return(false, updateresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                var rolesresult = await this._userManager.AddToRolesAsync(userresult.User, new[] { "User" });

                if (!rolesresult.Succeeded)
                {
                    return(false, rolesresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }

                var confirmmailresult = await _useremailmanager.GenerateEmailConfirmation(userresult.User.Id);

                if (!confirmmailresult.Succeeded)
                {
                    return(false, new List <FieldValidationErrorDTO>()
                    {
                        new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(confirmmailresult.Error.Key), confirmmailresult.Error.Description)
                    });
                }

                scope.Complete();
                return(true, new List <FieldValidationErrorDTO>());
            }
        }
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> Enable2FA(string userid, string token)
        {
            if (string.IsNullOrWhiteSpace(userid) || string.IsNullOrWhiteSpace(token))
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserDTO.OldPasswordClear), "You must provide a user id & authenticator token to enable 2FA!")
                });
            }

            var user = await _userManager.FindByIdAsync(userid);

            if (user == null)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user does not exist!")
                });
            }
            if (!user.EmailConfirmed)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user email must be confirmed!")
                });
            }
            if (user.TwoFactorEnabled)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user is already enrolled for 2FA!")
                });
            }

            var authresult = await ConfirmAuthenticatorCode(userid, token);

            if (authresult.Succeeded)
            {
                if (!user.TwoFactorEnabled)
                {
                    var updateResult = await _userManager.SetTwoFactorEnabledAsync(user, true);

                    if (!updateResult.Succeeded)
                    {
                        return(false, updateResult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                    }
                }
                return(true, new List <FieldValidationErrorDTO>());
            }
            return(false, new List <FieldValidationErrorDTO>()
            {
                new FieldValidationErrorDTO(nameof(UserSignInDTO.VerificationCode), "Authenticator code could not be validated!")
            });
        }
        public async Task <(bool Success, IEnumerable <FieldValidationErrorDTO> Errors)> Disable2FA(string userid)
        {
            if (string.IsNullOrWhiteSpace(userid))
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserDTO.OldPasswordClear), "You must provide a user id to enable 2FA!")
                });
            }

            var user = await _userManager.FindByIdAsync(userid);

            if (user == null)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user does not exist!")
                });
            }
            if (!user.EmailConfirmed)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user email must be confirmed!")
                });
            }
            if (!user.TwoFactorEnabled)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user is not enrolled for 2FA!")
                });
            }

            var disableresult = await _userManager.SetTwoFactorEnabledAsync(user, false);

            if (!disableresult.Succeeded)
            {
                return(false, disableresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
            }

            var resetresult = await _userManager.ResetAuthenticatorKeyAsync(user);

            if (!resetresult.Succeeded)
            {
                return(false, resetresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
            }

            return(true, new List <FieldValidationErrorDTO>());
        }
Esempio n. 5
0
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> ConfirmPhoneNumber(string userid, string token)
        {
            if (string.IsNullOrWhiteSpace(userid) || string.IsNullOrEmpty(token))
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserDTO.OldPasswordClear), "You must provide a user id & verification token to confirm sms verification code!")
                });
            }

            var user = await _userManager.FindByIdAsync(userid);

            if (user == null)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user does not exist!")
                });
            }
            if (user.PhoneNumberConfirmed)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(string.Empty, "The phone number for this user account has already been confirmed!")
                });
            }

            var phonesuccess = await _smsverifier.VerifyPhoneNumber(user.PhoneNumber, token);

            if (phonesuccess)
            {
                user.PhoneNumberConfirmed = true;
                var updateResult = await _userManager.UpdateAsync(user);

                if (!updateResult.Succeeded)
                {
                    return(false, updateResult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                }
                return(true, new List <FieldValidationErrorDTO>());
            }
            return(false, new List <FieldValidationErrorDTO>()
            {
                new FieldValidationErrorDTO("VerificationCode", "Verification code could not be validated!")
            });
        }
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> ConfirmEmailAddress(string userid, string token)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                if (string.IsNullOrWhiteSpace(userid))
                {
                    return(false, new List <FieldValidationErrorDTO>()
                    {
                        new FieldValidationErrorDTO(nameof(UserDTO.OldPasswordClear), "You must provide a user id to confirm email!")
                    });
                }

                var user = await _userManager.FindByIdAsync(userid);

                if (user == null)
                {
                    return(false, new List <FieldValidationErrorDTO>()
                    {
                        new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user does not exist!")
                    });
                }

                if (!user.EmailConfirmed)
                {
                    var result = await _userManager.ConfirmEmailAsync(user, token);

                    if (!result.Succeeded)
                    {
                        return(false, result.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                    }

                    user.EmailConfirmedTimestamp = DateTime.UtcNow;
                    var updateresult = await _userManager.UpdateAsync(user);

                    if (!updateresult.Succeeded)
                    {
                        return(false, updateresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
                    }
                }

                scope.Complete();
                return(true, new List <FieldValidationErrorDTO>());
            }
        }
Esempio n. 7
0
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> ResetPassword(Guid userid, string username, string password, string resettoken, string verifycode)
        {
            if (string.IsNullOrWhiteSpace(username) || userid == Guid.Empty || string.IsNullOrWhiteSpace(password))
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserPasswordChangeDTO.OldPassword), "You must provide both username, userid & new password in order to reset password!")
                });
            }

            var user = await _userManager.FindByIdAsync(userid.ToString());

            if (user == null)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserPasswordChangeDTO.Username), "Specified user does not exist!")
                });
            }
            if (user.UserName.ToLowerInvariant() != username.ToLowerInvariant())
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserPasswordChangeDTO.Username), "Specified username does not match the password reset request!")
                });
            }

            if (user.TwoFactorEnabled)
            {
                var tfaresult = await _userauthManager.ConfirmAuthenticatorCode(user.Id, verifycode);

                if (!tfaresult.Succeeded)
                {
                    return(false, new List <FieldValidationErrorDTO>()
                    {
                        new FieldValidationErrorDTO(nameof(UserSignInDTO.VerificationCode), "Your authentication code could not be validated!")
                    });
                }
            }

            var resetresult = await _userManager.ResetPasswordAsync(user, resettoken, password);

            if (!resetresult.Succeeded)
            {
                return(false, resetresult.Errors.Select(x => new FieldValidationErrorDTO(x.Code, x.Description)));
            }

            var changeconfresult = await GeneratePasswordChangeNotification(user);

            if (!changeconfresult.Succeeded)
            {
                return(false, changeconfresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Key), x.Description)));
            }

            return(true, new List <FieldValidationErrorDTO>());
        }
        public async Task <(bool Succeeded, IEnumerable <FieldValidationErrorDTO> Errors)> UploadAvatar(string userid, byte[] payload)
        {
            if (string.IsNullOrWhiteSpace(userid) || payload == null)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserDTO.OldPasswordClear), "You must provide a user and image to upload an avatar!")
                });
            }

            var user = await _userManager.FindByIdAsync(userid);

            if (user == null)
            {
                return(false, new List <FieldValidationErrorDTO>()
                {
                    new FieldValidationErrorDTO(nameof(UserSignInDTO.SuppliedUserName), "Specified user does not exist!")
                });
            }

            user.ProfileAvatar = payload;
            var updateresult = await _userManager.UpdateAsync(user);

            if (!updateresult.Succeeded)
            {
                return(false, updateresult.Errors.Select(x => new FieldValidationErrorDTO(FieldMappingHelper.MapErrorCodeToKey(x.Code), x.Description)));
            }

            return(true, new List <FieldValidationErrorDTO>());
        }