public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel forgotPassword)
        {
            var recaptcha          = new ReCaptcha();
            var responseFromServer = recaptcha.OnActionExecuting();

            if (responseFromServer.StartsWith("true", StringComparison.Ordinal))
            {
                if (forgotPassword != null && ModelState.IsValid)
                {
                    var userByUsername = await DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.UserName", forgotPassword.UserName }
                    }).ToListAsync();

                    if (userByUsername.Count > 0)
                    {
                        if (userByUsername[0].Account.Email.Equals(forgotPassword.Email))
                        {
                            var password              = CreatePassword();
                            var passwordEncryption    = new PasswordHashAndSalt();
                            var tempEncryptedPassword = passwordEncryption.getHashedPassword(password);
                            userByUsername[0].Account.TempPassword = tempEncryptedPassword;
                            await DatabaseContext.RegisteredUsers.ReplaceOneAsync(r => r.Account.UserName == userByUsername[0].Account.UserName, userByUsername[0]);

                            using (var mail = new MailMessage())
                            {
                                mail.To.Add(forgotPassword.Email);
                                mail.Subject = "Royal Holloway LETS Password Recovery";
                                mail.Body    = "<p>Hello " + userByUsername[0].About.FirstName + ",</p><h3>Forgotten your password?</h3><p>We got a request to reset your Royal Holloway LETS account's password.<br/>You use the below code in bold to login to your account.<br/><b>Please change your password to something memorable when you have logged in.</b></p><h2>" + password + "</h2><p>All the best,<br/>Royal Holloway LETS</p>";
                                SendEmail(mail);
                                ModelState.AddModelError("Success", "Please check you email, We have sent you your recovery password to your account.");
                                forgotPassword.UserName = null;
                                forgotPassword.Email    = null;
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Email", "Sorry, The Email you provided is not associated with the username you entered.");
                            return(View(forgotPassword));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "Sorry, We didn't find any account associated with this username in our system.");
                    }
                }
            }
            else
            {
                ModelState.AddModelError("ReCaptcha", "Incorrect CAPTCHA entered.");
                return(View(forgotPassword));
            }
            return(View());
        }
        public async Task <ActionResult> ForgotUsername(ForgotUsernameViewModel forgotUsername)
        {
            var recaptcha          = new ReCaptcha();
            var responseFromServer = recaptcha.OnActionExecuting();

            if (responseFromServer.StartsWith("true", StringComparison.Ordinal))
            {
                if (forgotUsername != null && ModelState.IsValid)
                {
                    var userByEmail = await DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.Email", forgotUsername.Email }
                    }).ToListAsync();

                    if (userByEmail.Count > 0)
                    {
                        using (var mail = new MailMessage())
                        {
                            mail.To.Add(forgotUsername.Email);
                            mail.Subject = "Royal Holloway LETS Username Recovery";
                            mail.Body    = "<p>Hello " + userByEmail[0].About.FirstName + ",</p><h3>Forgotten your username?</h3><p>We got a request about your Royal Holloway LETS account's username.<br/>Please find your username highlighted in bold below.<br/></p><h2>" + userByEmail[0].Account.UserName + "</h2><p>All the best,<br/>Royal Holloway LETS</p>";
                            SendEmail(mail);
                            ModelState.AddModelError("Success", "Please check you email, We have sent you your username.");
                            forgotUsername.Email = null;
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Email", "Sorry, The Email you provided is not present in our system.");
                        return(View(forgotUsername));
                    }
                }
            }
            else
            {
                ModelState.AddModelError("ReCaptcha", "Incorrect CAPTCHA entered.");
                return(View(forgotUsername));
            }
            return(View());
        }
        public ActionResult Register(RegisterUserViewModel registerUser)
        {
            var recaptcha          = new ReCaptcha();
            var responseFromServer = recaptcha.OnActionExecuting();

            if (responseFromServer.StartsWith("true", StringComparison.Ordinal))
            {
                if (registerUser != null && ModelState.IsValid)
                {
                    var userByUsername = DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.UserName", registerUser.Account.UserName }
                    }).ToList();

                    var userByEmail = DatabaseContext.RegisteredUsers.Find(new BsonDocument {
                        { "Account.Email", registerUser.Account.Email }
                    }).ToList();

                    if (userByUsername.Count == 0)
                    {
                        if (userByEmail.Count == 0)
                        {
                            var passwordEncryption = new PasswordHashAndSalt();
                            registerUser.Id = Guid.NewGuid().ToString();
                            registerUser.Account.Password        = passwordEncryption.getHashedPassword(registerUser.Account.Password);
                            registerUser.Account.ConfirmPassword = passwordEncryption.getHashedPassword(registerUser.Account.ConfirmPassword);
                            registerUser.Account.ImageId         = "586a7d67cf43d7340cb54670";
                            var tradingDetails = new LetsTradingDetails {
                                Id = registerUser.Id, Credit = 100
                            };
                            DatabaseContext.RegisteredUsers.InsertOne(registerUser);
                            DatabaseContext.LetsTradingDetails.InsertOne(tradingDetails);

                            using (var mail = new MailMessage())
                            {
                                mail.To.Add(registerUser.Account.Email);
                                mail.Subject = "Welcome to Royal Holloway LETS";
                                mail.Body    = "<p>Hello " + registerUser.About.FirstName + ",</p><h3>Thanks for joining Royal Holloway LETS</h3><p>Please find your account details below</p><p>Title : <b>" + registerUser.About.Title + "</b></p><p>First Name : <b>" + registerUser.About.FirstName + "</b></p><p>Last Name : <b>" + registerUser.About.LastName + "</b></p><p>Gender : <b>" + registerUser.About.Gender + "</b></p><p>User Name : <b>" + registerUser.Account.UserName + "</b></p><p>Kind Regards,<br/>Royal Holloway LETS</p>";
                                SendEmail(mail);
                                TempData.Add("Registered", "You have successfully signed up for Royal Holloway LETS, We have also sent you can email with your account details for your future reference.");
                            }

                            return(RedirectToAction("Login"));
                        }
                        else
                        {
                            registerUser.Account.Password        = null;
                            registerUser.Account.ConfirmPassword = null;
                            ModelState.AddModelError("Account.Email", "Sorry, The following email already exists in our system.");
                            return(View(registerUser));
                        }
                    }
                    else
                    {
                        registerUser.Account.Password        = null;
                        registerUser.Account.ConfirmPassword = null;
                        ModelState.AddModelError("Account.UserName", "Sorry, This username is not available.");

                        if (userByEmail.Count > 0)
                        {
                            ModelState.AddModelError("Account.Email", "Sorry, The following email already exists in our system.");
                        }

                        return(View(registerUser));
                    }
                }
            }
            else
            {
                registerUser.Account.Password        = null;
                registerUser.Account.ConfirmPassword = null;
                ModelState.AddModelError("ReCaptcha", "Incorrect CAPTCHA entered.");
                return(View(registerUser));
            }
            return(View());
        }