public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            log.LogDebug("ExternalLoginCallback called with returnurl " + returnUrl);

            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                return View(nameof(Login));
            }

            var info = await signInManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                log.LogDebug("ExternalLoginCallback redirecting to login because GetExternalLoginInfoAsync returned null ");
                return RedirectToAction("Login");
            }
            
            // Sign in the user with this external login provider if the user already has a login.
            var result = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
            if (result.Succeeded)
            {
                //TODO: how to get the user here?
                //await ipAddressTracker.TackUserIpAddress(Site.SiteGuid, user.UserGuid);

                log.LogDebug("ExternalLoginCallback ExternalLoginSignInAsync succeeded ");
                if (!string.IsNullOrEmpty(returnUrl))
                {
                    return LocalRedirect(returnUrl);
                }

                return this.RedirectToSiteRoot(Site);
            }
            if (result.RequiresTwoFactor)
            {
                log.LogDebug("ExternalLoginCallback ExternalLoginSignInAsync RequiresTwoFactor ");
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl });
            }
            if (result.IsLockedOut)
            {
                log.LogDebug("ExternalLoginCallback ExternalLoginSignInAsync IsLockedOut ");
                return View("Lockout");
            }
            else
            {
                log.LogDebug("ExternalLoginCallback needs new account ");
                // If the user does not have an account, then ask the user to create an account.
                ViewData["ReturnUrl"] = returnUrl;
                ViewData["LoginProvider"] = info.LoginProvider;
                var email = info.Principal.FindFirstValue(ClaimTypes.Email);
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
            }

        }
Beispiel #2
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null)
        {
            log.LogInformation("ExternalLoginCallback called with returnurl " + returnUrl);

            var info = await signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                log.LogInformation("ExternalLoginCallback redirecting to login because GetExternalLoginInfoAsync returned null ");
                return(RedirectToAction("Login"));
            }

            // Sign in the user with this external login provider if the user already has a login.
            var result = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false);

            if (result.Succeeded)
            {
                log.LogInformation("ExternalLoginCallback ExternalLoginSignInAsync succeeded ");
                return(this.RedirectToLocal(returnUrl));
            }
            if (result.RequiresTwoFactor)
            {
                log.LogInformation("ExternalLoginCallback ExternalLoginSignInAsync RequiresTwoFactor ");
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl }));
            }
            if (result.IsLockedOut)
            {
                log.LogInformation("ExternalLoginCallback ExternalLoginSignInAsync IsLockedOut ");
                return(View("Lockout"));
            }
            else
            {
                log.LogInformation("ExternalLoginCallback needs new account ");
                // If the user does not have an account, then ask the user to create an account.
                ViewData["ReturnUrl"]     = returnUrl;
                ViewData["LoginProvider"] = info.LoginProvider;
                var email = info.ExternalPrincipal.FindFirstValue(ClaimTypes.Email);
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    Email = email
                }));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> LinkLoginCallback()
        {
            var user = await userManager.FindByIdAsync(HttpContext.User.GetUserId());

            if (user == null)
            {
                return(View("Error"));
            }
            var info = await signInManager.GetExternalLoginInfoAsync(User.GetUserId());

            if (info == null)
            {
                this.AlertDanger("oops something went wrong please try again");
                return(RedirectToAction("ManageLogins"));
            }
            var result = await userManager.AddLoginAsync(user, info);

            if (!result.Succeeded)
            {
                this.AlertDanger("oops something went wrong, please try again");
            }

            return(RedirectToAction("ManageLogins"));
        }
Beispiel #4
0
        public async Task <UserLoginResult> TryExternalLogin(string providedEmail = "", bool?didAcceptTerms = null)
        {
            var          template    = new LoginResultTemplate();
            IUserContext userContext = null;
            var          email       = providedEmail;

            template.ExternalLoginInfo = await signInManager.GetExternalLoginInfoAsync();

            if (template.ExternalLoginInfo == null)
            {
                template.RejectReasons.Add("signInManager.GetExternalLoginInfoAsync returned null");
            }
            else
            {
                template.User = await userManager.FindByLoginAsync(template.ExternalLoginInfo.LoginProvider, template.ExternalLoginInfo.ProviderKey);

                if (template.User == null)
                {
                    if (string.IsNullOrWhiteSpace(email))
                    {
                        email = template.ExternalLoginInfo.Principal.FindFirstValue(ClaimTypes.Email);
                    }

                    if (!string.IsNullOrWhiteSpace(email) && email.Contains("@"))
                    {
                        template.User = await userManager.FindByNameAsync(email);
                    }
                }

                if (template.User == null)
                {
                    template.User = await CreateUserFromExternalLogin(template.ExternalLoginInfo, email, didAcceptTerms);
                }
            }

            if (template.User != null)
            {
                await loginRulesProcessor.ProcessAccountLoginRules(template);
            }

            if (template.SignInResult == SignInResult.Failed && template.User != null && template.RejectReasons.Count == 0)
            {
                template.SignInResult = await signInManager.ExternalLoginSignInAsync(template.ExternalLoginInfo.LoginProvider, template.ExternalLoginInfo.ProviderKey, isPersistent : false);

                if (template.SignInResult.Succeeded)
                {
                    // TODO:
                    //update last login time
                }
            }

            if (template.User != null &&
                template.SignInResult != SignInResult.Success &&
                template.SignInResult != SignInResult.TwoFactorRequired)
            {
                //clear the external login
                await signInManager.SignOutAsync();
            }

            if (template.User != null)
            {
                userContext = new UserContext(template.User);
            }

            return(new UserLoginResult(
                       template.SignInResult,
                       template.RejectReasons,
                       userContext,
                       template.MustAcceptTerms,
                       template.NeedsAccountApproval,
                       template.NeedsEmailConfirmation,
                       template.EmailConfirmationToken,
                       template.NeedsPhoneConfirmation,
                       template.ExternalLoginInfo
                       ));
        }