Esempio n. 1
0
        public async Task <IActionResult> ExternalLoginConfirmation(MvcExternalLoginViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                IdentityResult result;

                // Get the information about the user from the external login provider
                ExternalLoginInfo externalLoginInfo = await _signInManager.GetExternalLoginInfoAsync();

                if (externalLoginInfo == null)
                {
                    throw new CustomApplicationException("Error loading external login information during confirmation.");
                }

                ApplicationUser user = new ApplicationUser {
                    UserName = model.Username, Email = model.Email
                };
                ApplicationUser existingUser = await _userManager.FindByEmailAsync(model.Email);

                if (existingUser != null)
                {
                    user = existingUser;
                }
                else
                {
                    await _userManager.CreateAsync(user);
                }

                result = await _userManager.AddLoginAsync(user, externalLoginInfo);

                if (result.Succeeded)
                {
                    return(await HandleSucessfullExternalLogin(returnUrl, externalLoginInfo, user, existingUser));
                }

                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }
Esempio n. 2
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                ErrorMessage = $"Error from external provider: {remoteError}";
                return(RedirectToAction(nameof(Login)));
            }
            ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(RedirectToAction(nameof(Login)));
            }

            // Sign in the user with this external login provider if the user already has a login.
            Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                string          email        = info.Principal.FindFirstValue(ClaimTypes.Email);
                ApplicationUser existingUser = await _userManager.FindByEmailAsync(email);

                if (existingUser != null)
                {
                    string logMessage = String.Format("User {0} logged in with {1} provider.", existingUser.UserName, info.LoginProvider);

                    await NotificationSender.SendTeamNotificationAsync(logMessage);

                    _logger.LogInformation(logMessage);

                    SetProfileOnSession(new Guid(existingUser.Id), existingUser.UserName);

                    await SetStaffRoles(existingUser);

                    SetPreferences(existingUser);
                }

                return(RedirectToLocal(returnUrl));
            }

            if (result.IsLockedOut)
            {
                return(RedirectToAction(nameof(Lockout)));
            }
            else
            {
                // If the user does not have an account, then ask the user to create an account.
                ViewData["ReturnUrl"]     = returnUrl;
                ViewData["LoginProvider"] = info.LoginProvider;
                ViewData["ButtonText"]    = SharedLocalizer["Register"];
                string text = "You've successfully authenticated with your external account. Please choose a username to use and click the Register button to finish logging in.";

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

                MvcExternalLoginViewModel model = new MvcExternalLoginViewModel {
                    Email = email
                };

                ApplicationUser existingUser = await _userManager.FindByEmailAsync(email);

                if (existingUser != null)
                {
                    model.UserExists = true;
                    model.Username   = existingUser.UserName;

                    text = "Oh! It looks like you already have a user registered here with us. Check your info below and confirm to link your account to your external account.";

                    ViewData["ButtonText"] = SharedLocalizer["Link Acounts"];
                }
                else
                {
                    model.ProfileName = SelectName(info);
                }

                ViewData["RegisterText"] = SharedLocalizer[text];

                return(View("ExternalLogin", model));
            }
        }