コード例 #1
0
        public async Task <IActionResult> LinkLoginCallback()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound(_localizer["UserNotFound", _userManager.GetUserId(User)]));
            }

            var info = await _signInManager.GetExternalLoginInfoAsync(user.Id.ToString());

            if (info == null)
            {
                throw new ApplicationException(_localizer["ErrorLoadingExternalLogin", user.Id]);
            }

            var result = await _userManager.AddLoginAsync(user, info);

            if (!result.Succeeded)
            {
                AddErrors(result);
                return(View("LinkLoginFailure"));
            }

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            StatusMessage = _localizer["ExternalLoginAdded"];

            return(RedirectToAction(nameof(ExternalLogins)));
        }
コード例 #2
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
        {
            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 TUser
                {
                    UserName = model.UserName,
                    Email    = model.Email
                };

                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }

                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;

            return(View(model));
        }