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));
            }

            // this is actually signing the user in
            var info = await signInManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                log.LogDebug("ExternalLoginCallback redirecting to login because GetExternalLoginInfoAsync returned null ");
                return RedirectToAction(nameof(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(nameof(SendCode), new { ReturnUrl = returnUrl });
            }

            if (result.IsNotAllowed)
            {
                return RedirectToAction("PendingApproval");
            }
            
            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);
                var model = new ExternalLoginConfirmationViewModel();
                model.Email = email;
                model.RegistrationPreamble = Site.RegistrationPreamble;
                model.RegistrationAgreement = Site.RegistrationAgreement;
                model.AgreementRequired = Site.RegistrationAgreement.Length > 0;
                return View("ExternalLoginConfirmation", model);
            }

        }
        public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            log.LogDebug("ExternalLoginConfirmation called with returnurl " + returnUrl);

            //if (signInManager.IsSignedIn(User))
            //{
            //    return RedirectToAction("Index", "Manage");
            //}

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await signInManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }

                var userName = model.Email.Replace("@", string.Empty).Replace(".", string.Empty);
                var userNameAvailable = await userManager.LoginIsAvailable(Guid.Empty, userName);
                if (!userNameAvailable)
                {
                    userName = model.Email;
                }

                var user = new SiteUser {
                    SiteId = Site.Id,
                    UserName = userName,
                    Email = model.Email,
                    AccountApproved = Site.RequireApprovalBeforeLogin ? false : true
                };
                var result = await userManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    log.LogDebug("ExternalLoginConfirmation user created ");

                    await ipAddressTracker.TackUserIpAddress(Site.Id, user.Id);

                    result = await userManager.AddLoginAsync(user, info);
                    if (result.Succeeded)
                    {
                        log.LogDebug("ExternalLoginConfirmation AddLoginAsync succeeded ");

                        
                        if (Site.RequireConfirmedEmail) // require email confirmation
                        {
                            var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                            var callbackUrl = Url.Action(new UrlActionContext
                            {
                                Action = "ConfirmEmail",
                                Controller = "Account",
                                Values = new { userId = user.Id.ToString(), code = code },
                                Protocol = HttpContext.Request.Scheme
                            });

                            emailSender.SendAccountConfirmationEmailAsync(
                                Site,
                                model.Email,
                                sr["Confirm your account"],
                                callbackUrl).Forget();

                            // this is needed to clear the external cookie - wasn't needed in rc2
                            await signInManager.SignOutAsync();

                            if (this.SessionIsAvailable())
                            {
                                this.AlertSuccess(sr["Please check your email inbox, we just sent you a link that you need to click to confirm your account"], true);

                                return Redirect("/");
                            }
                            else
                            {
                                return RedirectToAction("EmailConfirmationRequired", new { userId = user.Id, didSend = true });
                            }
                        }
                        else
                        {
                            if (Site.RequireApprovalBeforeLogin)
                            {
                                emailSender.AccountPendingApprovalAdminNotification(Site, user).Forget();

                                // this is needed to clear the external cookie - wasn't needed in rc2
                                await signInManager.SignOutAsync();

                                return RedirectToAction("PendingApproval", new { userId = user.Id, didSend = true });
                            }
                            else
                            {
                                await signInManager.SignInAsync(user, isPersistent: false);

                                if (!string.IsNullOrEmpty(returnUrl))
                                {
                                    return LocalRedirect(returnUrl);
                                }

                                return this.RedirectToSiteRoot(Site);
                            }
                        }

                        
                    }
                    else
                    {
                        log.LogDebug("ExternalLoginConfirmation AddLoginAsync failed ");
                    }
                }
                else
                {
                    log.LogDebug("ExternalLoginConfirmation failed to user created ");
                }

                AddErrors(result);
            }
            else
            {
                log.LogDebug("ExternalLoginConfirmation called with ModelStateInvalid ");
                model.RegistrationPreamble = Site.RegistrationPreamble;
                model.RegistrationAgreement = Site.RegistrationAgreement;
                model.AgreementRequired = Site.RegistrationAgreement.Length > 0;
            }

            ViewData["ReturnUrl"] = returnUrl;
            return View(model);
        }
示例#3
0
        public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            log.LogInformation("ExternalLoginConfirmation called with returnurl " + returnUrl);

            if (User.IsSignedIn())
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await signInManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new SiteUser {
                    SiteGuid = Site.SiteGuid,
                    SiteId = Site.SiteId,
                    UserName = model.Email,
                    Email = model.Email
                };
                var result = await userManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    log.LogInformation("ExternalLoginConfirmation user created ");

                    result = await userManager.AddLoginAsync(user, info);
                    if (result.Succeeded)
                    {
                        log.LogInformation("ExternalLoginConfirmation AddLoginAsync succeeded ");
                        await signInManager.SignInAsync(user, isPersistent: false);
                        return this.RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        log.LogInformation("ExternalLoginConfirmation AddLoginAsync failed ");
                    }
                }
                else
                {
                    log.LogInformation("ExternalLoginConfirmation failed to user created ");
                }

                AddErrors(result);
            }
            else
            {
                log.LogInformation("ExternalLoginConfirmation called with ModelStateInvalid ");
            }

            ViewData["ReturnUrl"] = returnUrl;
            return View(model);
        }