示例#1
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            LoginViewModel loginViewModel = new LoginViewModel
            {
                ReturnUrl      = returnUrl,
                ExternalLogins =
                    (await Signinmanager.GetExternalAuthenticationSchemesAsync()).ToList()
            };

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

                return(View("Login", loginViewModel));
            }

            // Get the login information about the user from the external login provider
            var info = await Signinmanager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                ModelState
                .AddModelError(string.Empty, "Error loading external login information.");

                return(View("Login", loginViewModel));
            }

            // If the user already has a login (i.e if there is a record in AspNetUserLogins
            // table) then sign-in the user with this external login provider
            var signInResult = await Signinmanager.ExternalLoginSignInAsync(info.LoginProvider,
                                                                            info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (signInResult.Succeeded)
            {
                return(LocalRedirect(returnUrl));
            }
            // If there is no record in AspNetUserLogins table, the user may not have
            // a local account
            else
            {
                // Get the email claim value
                var email = info.Principal.FindFirstValue(ClaimTypes.Email);

                if (email != null)
                {
                    // Create a new user without password if we do not have a user already
                    var user = await Usermanager.FindByEmailAsync(email);

                    if (user == null)
                    {
                        user = new ApplicationUser
                        {
                            UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
                            Email    = info.Principal.FindFirstValue(ClaimTypes.Email)
                        };

                        await Usermanager.CreateAsync(user);

                        await Usermanager.AddToRoleAsync(user, "User");
                    }

                    // Add a login (i.e insert a row for the user in AspNetUserLogins table)
                    await Usermanager.AddLoginAsync(user, info);

                    await Signinmanager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                // If we cannot find the user email we cannot continue
                ViewBag.ErrorTitle   = $"Email claim not received from: {info.LoginProvider}";
                ViewBag.ErrorMessage = "Please contact support on " + Configuration["Email"];

                return(View("Error"));
            }
        }