Beispiel #1
0
        public async Task <IActionResult> GoogleResponse()
        {
            ExternalLoginInfo info = await AppSignInManager.GetExternalLoginInfoAsync();

            var result = await AppSignInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, false);

            string[] userInfo = { info.Principal.FindFirst(ClaimTypes.Name).Value, info.Principal.FindFirst(ClaimTypes.Email).Value };
            if (result.Succeeded)
            {
                return(LocalRedirect("/"));
            }
            else
            {
                AppUser user = new AppUser
                {
                    Email    = info.Principal.FindFirst(ClaimTypes.Email).Value,
                    UserName = info.Principal.FindFirst(ClaimTypes.Email).Value,
                };

                IdentityResult identResult = await AppUserManager.CreateAsync(user);

                if (identResult.Succeeded)
                {
                    identResult = await AppUserManager.AddLoginAsync(user, info);

                    if (identResult.Succeeded)
                    {
                        await AppSignInManager.SignInAsync(user, false);

                        return(LocalRedirect("/"));
                    }
                }
                return(LocalRedirect("/"));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> ExternalLoginCallBack(string returnTo = null, string remoteError = null)
        {
            if (!string.IsNullOrEmpty(remoteError) || !string.IsNullOrWhiteSpace(remoteError))
            {
                return(View("Error"));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(View("Error"));
            }

            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, false);

            if (result.Succeeded)
            {
                await _signInManager.UpdateExternalAuthenticationTokensAsync(info);

                return(RedirectToLocal(returnTo));
            }

            if (result.IsLockedOut)
            {
                return(View("LockOut"));
            }

            if (result.RequiresTwoFactor)
            {
                return(RedirectToRoute("GetSendCode", new { returnTo }));
            }
            else
            {
                ViewData["returnTo"] = returnTo;


                var email = info.Principal.FindFirstValue(ClaimTypes.Email);

                var model = new ExternalLoginConfirm
                {
                    Email = email
                };

                return(View("ExternalLoginConfirm", model));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ErrorMessage = "Error loading external login information.";
                return(RedirectToPage("./Login", new { ReturnUrl = returnUrl }));
            }

            // 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, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider);
                return(LocalRedirect(returnUrl));
            }
            if (result.IsLockedOut)
            {
                return(RedirectToPage("./Lockout"));
            }
            else
            {
                // If the user does not have an account, then ask the user to create an account.
                ReturnUrl     = returnUrl;
                LoginProvider = info.LoginProvider;
                if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
                {
                    Input = new InputModel
                    {
                        Email = info.Principal.FindFirstValue(ClaimTypes.Email)
                    };
                }
                return(Page());
            }
        }