예제 #1
0
        public virtual async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await _authenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction(MVC.Account.Login()));
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await _signInManager.ExternalSignInAsync(loginInfo, isPersistent : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(RedirectToAction(MVC.Error.LockOut()));

            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return(View(MVC.Account.Views.ExternalLoginConfirmation, new ExternalLoginConfirmationViewModel {
                    Email = loginInfo.Email
                }));
            }
        }
예제 #2
0
        public async Task <ExternalLoginResultOutput> ExecuteAsync(string scheme)
        {
            var authResult = await _authenticationService.AuthenticateAsync(scheme);

            if (!authResult.Succeeded)
            {
                throw authResult.Failure;
            }

            var emailClaim    = authResult.Principal.FindFirst(ClaimTypes.Email);
            var correlationId = Guid.NewGuid();
            var account       = await _accountProviderService.ProvideAccountForExternalLoginAsync(emailClaim.Value, correlationId);

            if (account.DomainEvents.Any(x => x.GetType() == typeof(AccountCreatedDomainEvent)))
            {
                var pictureClaim = authResult.Principal.FindFirst("picture");
                var picture      = pictureClaim != null ? pictureClaim.Value : string.Empty;
                var accountCreatedIntegrationEvent = new AccountCreatedIntegrationEvent(correlationId, account.Id, account.Email, picture);
                await _integrationEventBus.PublishIntegrationEventAsync(accountCreatedIntegrationEvent);
            }

            var claims = await _accountClaimsCreatorService.CreateAccountClaimsAsync(account);

            var externalSignInTask = _signInService.ExternalSignInAsync(account.Id, account.Email, scheme, claims);
            var signOutTask        = _signOutService.SignOutAsync(scheme);

            var returnUrl =
                authResult.Items is null || !authResult.Items.ContainsKey("returnUrl") ||
                string.IsNullOrWhiteSpace(authResult.Items["returnUrl"])
                    ? "~/"
                    : authResult.Items["returnUrl"];

            var authRequest = await _authorizationService.GetAuthorizationRequestAsync(returnUrl);

            await Task.WhenAll(externalSignInTask, signOutTask);

            return(new ExternalLoginResultOutput(returnUrl, authRequest?.IsNativeClient));
        }