示例#1
0
        public async Task <IActionResult> OnGetAddLinkCallbackAsync(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (remoteError != null)
            {
                StatusMessage = $"Error from external provider: {remoteError}";
                return(Page());
            }
            var info = await _externalLoginService.GetCurrentLoginContextAsync();

            if (info == null)
            {
                StatusMessage = "Error loading external login information during confirmation";
                return(Page());
            }
            var userId = _currentUserService.UserId;
            var result = await _externalLoginService.AddLoginAsync(userId);

            if (result.Succeeded)
            {
                StatusMessage = $"The {info.ProviderName} account was successfully linked";
                _logger.LogInformation("User with ID '{UserId}' linked {ProviderName} account.", userId, info.ProviderName);
                return(RedirectToPage());
            }
            else
            {
                StatusMessage = "An error occured while linking the account";
                return(Page());
            }
        }
示例#2
0
        public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            // Get the information about the user from the external login provider
            var info = await _externalLoginService.GetCurrentLoginContextAsync();

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

            if (ModelState.IsValid)
            {
                (string userId, var result) = await _identityService.CreateUserAsync(Input.Email, Input.Password);

                if (result.Succeeded)
                {
                    result = await _externalLoginService.AddLoginAsync(userId);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User created an account using {Name} provider.", info.ProviderName);

                        (_, var token) = await _identityService.GenerateEmailConfirmationTokenAsync(Input.Email);

                        var code        = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));
                        var callbackUrl = Url.Page(
                            "/ConfirmEmail",
                            pageHandler: null,
                            values: new { userId = userId, code = code },
                            protocol: Request.Scheme);

                        await _emailSender.SendEmailAsync(new EmailRequest(Input.Email, "Confirm your email",
                                                                           $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."));

                        return(RedirectToPage("./RegisterConfirmation", new { Email = Input.Email }));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error);
                }
            }

            ProviderDisplayName = info.ProviderName;
            ReturnUrl           = returnUrl;
            return(Page());
        }
示例#3
0
        public async Task <IActionResult> HandleExternalLogin(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                return(BadRequest($"Error while login via external provider. Message = {remoteError}"));
            }

            var info = await loginService.GetExternalLoginInfoAsync();

            var result = await loginService.CheckExternalProviderSignIn(info.LoginProvider, info.ProviderKey);

            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            if (result)
            {
                var signedUser = await loginService.FindByEmailAsync(info.Principal.FindFirstValue(ClaimTypes.Email));

                return(new OkObjectResult(await jwtService.GetJwtToken(signedUser)));
            }

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

            if (string.IsNullOrEmpty(userEmail))
            {
                return(BadRequest($"Email scope access is required to add {info.ProviderDisplayName} provider"));
            }

            var user = await loginService.FindByEmailAsync(userEmail);

            if (user != null)
            {
                if (!user.EmailConfirmed)
                {
                    var token = await loginService.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Action("ConfirmExternalProvider", "Account",
                                                 values: new
                    {
                        userId              = user.Id,
                        code                = token,
                        loginProvider       = info.LoginProvider,
                        providerDisplayName = info.LoginProvider,
                        providerKey         = info.ProviderKey
                    },
                                                 protocol: HttpContext.Request.Scheme);

                    await emailService.SendEmailAsync(user.Email, $"Confirm {info.ProviderDisplayName} external login",
                                                      $"Please confirm association of your {info.ProviderDisplayName} " +
                                                      $"account by clicking <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>here</a>");

                    return(new OkObjectResult($"External account association with {info.ProviderDisplayName} is pending. Please check your email"));
                }

                await loginService.AddLoginAsync(user, info);

                return(new OkObjectResult(await jwtService.GetJwtToken(user)));
            }

            return(new OkObjectResult(new
            {
                Message = "Use this to associate your account with external provider",
                associate = userEmail,
                loginProvider = info.LoginProvider,
                providerDisplayName = info.ProviderDisplayName,
                providerKey = info.ProviderKey
            }));
        }