public async Task <IActionResult> Index()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            _organizationRepository.CurrentUser = user;

            // Add claims to or replace claims from dbo.AspNetUserClaims table.
            var identityResult = await _claimsHelper.AddOrReplaceClaimsAsync(user, User.Claims.ToList()); // Add or replace default claims.

            Organization organization = null;

            if (user.OrgId != null)
            {
                organization = _organizationRepository.Get(user.OrgId);
            }

            var model = new IndexViewModel
            {
                Username         = user.UserName,
                Email            = user.Email,
                PhoneNumber      = user.PhoneNumber,
                IsEmailConfirmed = user.EmailConfirmed,
                OrganizationName = organization != null ? organization.Name : string.Empty,
                StatusMessage    = StatusMessage
            };

            return(View(model));
        }
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                SignInResult result;

                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var user = await _userManager.FindByNameAsync(model.Email);

                if (user == null)
                {
                    result = SignInResult.Failed;
                }
                else
                {
                    // This is added after VS2017 template generated code.
                    if (!await _userManager.IsEmailConfirmedAsync(user))
                    {
                        ModelState.AddModelError("", "Email is not confirmed");
                        return(View(model));
                    }

                    result = await _signInManager.PasswordSignInAsync(user, model.Password, model.RememberMe, lockoutOnFailure : false);
                }

                if (result.Succeeded && user != null)
                {
                    // Add claims to or replace claims from dbo.AspNetUserClaims table.
                    var identityResult = await _claimsHelper.AddOrReplaceClaimsAsync(user); // Add or replace default claims.

                    _logger.LogInformation("User logged in.");
                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToAction(nameof(Lockout)));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(View(model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }