public async Task <ActionResult <UserDto> > Register(RegisterDto registerDto)
        {
            if (await UserExists(registerDto.Username))
            {
                return(BadRequest("Username already taken!"));
            }

            var user = _mapper.Map <AppUser>(registerDto);

            user.UserName = registerDto.Username.ToLower();
            user.Email    = registerDto.Email;

            var result = await _userManager.CreateAsync(user, registerDto.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(result.Errors));
            }

            var roleResult = await _userManager.AddToRoleAsync(user, "Operator");

            if (!roleResult.Succeeded)
            {
                return(BadRequest(roleResult.Errors));
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            string urlPath = "";
            var    env     = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            if (env.ToLower() == "development")
            {
                urlPath = _config["returnPaths:ConfirmEmail"];
            }
            else
            {
                urlPath = Environment.GetEnvironmentVariable("ReturnPaths:ConfirmEmail");
            }

            var confirmationLink = URLBuilder.BuildUrl(urlPath, token, user.Id.ToString());
            var message          = new Message(new string[] { user.Email }, "Confirmation Email Link",
                                               URLBuilder.BuildContext(URLBuilder.MessageType.ConfirmEmail, confirmationLink, user.UserName), null);
            await _emailSender.SendEmailAsync(message);

            return(new UserDto
            {
                Username = user.UserName,
                Token = await _tokenService.CreateToken(user),
                LastName = user.LastName,
                FirstName = user.FirstName,
                Created = user.Created
            });
        }
        public async Task <ActionResult> ForgotPassword(ForgotPasswordDto forgotPassword)
        {
            if (forgotPassword.Email == null)
            {
                return(BadRequest("Missing Email!"));
            }

            var user = await _userManager.Users
                       .IgnoreQueryFilters()
                       .Where(e => e.Email.ToLower() == forgotPassword.Email.ToLower())
                       .FirstOrDefaultAsync();

            if (user == null)
            {
                return(Unauthorized("Username not Found"));
            }

            var token = await _userManager.GeneratePasswordResetTokenAsync(user);

            string urlPath = "";

            if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").ToLower() == "development")
            {
                urlPath = _config["returnPaths:PasswordChange"];
            }
            else
            {
                urlPath = Environment.GetEnvironmentVariable("ReturnPaths:PasswordChange");
            }

            var changePasswordLink = URLBuilder.BuildUrl(urlPath, token, user.Id.ToString());

            var message = new Message(new string[] { user.Email }, "Reset Password link",
                                      URLBuilder.BuildContext(URLBuilder.MessageType.ResetPassword, changePasswordLink, user.UserName), null);
            await _emailSender.SendEmailAsync(message);

            return(Ok());
        }