public async Task <bool> forgotPasswordAsync(string email, string securityQuestion, string securityQuestionReply)
        {
            var myuser = await _context.Users.FirstOrDefaultAsync(x => x.Email == email);

            if (myuser == null)
            {
                throw new KeyNotFoundException();
            }
            else
            {
                if (myuser.SecurityQuestion.Equals(securityQuestion) &&
                    myuser.SecurityQuestionAnswer.Equals(securityQuestionReply))
                {
                    if (myuser.IsEmailVerified ?? false)
                    {
                        var tempPassword = Authentication.generateTempPassord();
                        myuser.PasswordHash        = Authentication.generateUserHash(tempPassword);
                        myuser.IsTemporaryPassword = true;
                        EmailOperations.sendPasswordChangeEmailAsync(tempPassword, myuser.Name, myuser.Email);
                        _context.Update(myuser);
                        await _context.SaveChangesAsync();

                        return(true);
                    }
                    else
                    {
                        var token = Authentication.generateEmailTokenHash();
                        myuser.VerificationToken       = token;
                        myuser.VerificationTokenExpiry = DateTime.UtcNow.AddDays(1);
                        EmailOperations.sendVerificationEmailAsync(myuser.Name, myuser.Email, token);
                    }
                }
                return(false);
            }
        }
Exemple #2
0
        public async Task <UserProfileDto?> UpdateUserAccountAsync(
            int locationId, IEnumerable <Claim> claims, UpdateNameEmailDto update)
        {
            var user_id = claims?.FirstOrDefault(x => x.Type == "ID");
            int id;

            if (!int.TryParse(user_id?.Value, out id))
            {
                return(null);
            }
            var user = await _context.Users
                       .Include(x => x.UserKeyMappings)
                       .ThenInclude(x => x.Keyholder)
                       .Include(x => x.ChangeEmailRequests)
                       .Include(x => x.UserLocations)
                       .FirstOrDefaultAsync(x => x.UserID == id);

            var keyholdemap = user?.UserKeyMappings?.FirstOrDefault(x => x.LocationId == locationId);

            if (keyholdemap == null)
            {
                return(null);
            }
            ;
            if (update.name != null)
            {
                user.Name = update.name;
            }
            Task save;

            if (update.email != null)
            {
                var email = EmailOperations.sendVerificationEmail(
                    user.Name, update.email, user.VerificationToken, true);
                user.Email                   = update.email;
                user.VerificationToken       = Authentication.generateEmailTokenHash();
                user.VerificationTokenExpiry = DateTime.UtcNow.AddDays(1);

                var changeRequest = new ChangeEmailRequest()
                {
                    User                    = user,
                    RequestedOn             = DateTime.UtcNow,
                    Email                   = update.email,
                    VerificationToken       = user.VerificationToken,
                    VerificationTokenExpiry = user.VerificationTokenExpiry,
                };
                await _context.EmailChangeRequests.AddAsync(changeRequest);

                _context.Users.Update(user);
                await email;
                save = _context.SaveChangesAsync();
            }
            else
            {
                _context.Users.Update(user);
                save = _context.SaveChangesAsync();
            }

            var model = _mapper.Map <UserProfileDto>(user);

            model.locationId = locationId;
            var tookit = _mapper.Map <ToolkitInfoDto>(keyholdemap.Keyholder);

            model.toolkitInfo = tookit;
            await save;

            return(model);
        }