public async Task <IActionResult> VerifyEmail(string token)
        {
            var email = await this.aimloginDb.UserEmail.FirstOrDefaultAsync(e => e.VerifyToken == token);

            if (email == null)
            {
                throw new Exception($"No email exists for verification token {token}.");
            }

            var userId = await this.aimLoginData.SingleValue <UserEmail>().ReverseLookupUserId(email);

            var user = await this.aimloginDb.Users.FindAsync(userId);

            var userInfo = await this.aimLoginData.SingleValue <UserLoginInfo>().GetOrDefaultAsync(user);

            if (userInfo.HasEmailBeenVerified)
            {
                return(RedirectToAction("Index", "Home"));
            }

            userInfo.HasEmailBeenVerified = true;
            await this.aimLoginData.SingleValue <UserLoginInfo>().SetAsync(user, userInfo);

            await HttpContext.AimSignInAsync(user, this.aimloginUsers);

            return(RedirectToAction("Index", "Home"));
        }
        public async Task <IActionResult> Login(LoginViewModel model)
        {
            if (model.ReturnUrl == null || !Url.IsLocalUrl(model.ReturnUrl))
            {
                model.ReturnUrl = "/";
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            User user;

            try
            {
                user = await this.aimloginUsers.GetUserByLogin(model.Username, model.Password);

                if (user == null)
                {
                    throw new Exception("Internal error: User object is null, yet GetUserByLogin passed.");
                }
            }
            catch (EmailNotVerifiedException)
            {
                return(RedirectToAction("VerifyEmail", "Home"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(View(model));
            }

            await HttpContext.AimSignInAsync(user, this.aimloginUsers);

            return(Redirect(model.ReturnUrl));
        }