src/NuGetGallery/Controllers/AuthenticationController.cs
/// <summary>
/// This is the challenge response action for linking or replacing external credentials
/// after external authentication.
/// </summary>
/// <param name="returnUrl">The url to return upon credential replacement</param>
/// <returns><see cref="ActionResult"/> for returnUrl</returns>
This is the challenge response action for linking or replacing external credentials
after external authentication.
This is the challenge response action for linking or replacing external credentials after external authentication .
[HttpGet]
        public virtual async Task<ActionResult> LinkOrChangeExternalCredential(string returnUrl)
        {
            var user = GetCurrentUser();
            var result = await _authService.ReadExternalLoginCredential(OwinContext);
            if (result?.Credential == null)
            {
                TempData["ErrorMessage"] = Strings.ExternalAccountLinkExpired;
                return SafeRedirect(returnUrl);
            }
            var newCredential = result.Credential;
            if (await _authService.TryReplaceCredential(user, newCredential))
            {
                await RemovePasswordCredential(user);
                var authenticatedUser = await _authService.Authenticate(newCredential);
                var usedMultiFactorAuthentication = result.LoginDetails?.WasMultiFactorAuthenticated ?? false;
                await _authService.CreateSessionAsync(OwinContext, authenticatedUser, usedMultiFactorAuthentication);
                var newEmailAddress = GetEmailAddressFromExternalLoginResult(result, out string errorReason);
                if (!string.IsNullOrEmpty(errorReason))
                {
                    TempData["ErrorMessage"] = errorReason;
                    return SafeRedirect(returnUrl);
                }
                var linkingDifferentEmailAddress = !newEmailAddress.Equals(user.EmailAddress, StringComparison.OrdinalIgnoreCase);
                TempData["Message"] = linkingDifferentEmailAddress
                    ? string.Format(Strings.ChangeCredential_SuccessDifferentEmail, newEmailAddress, user.EmailAddress)
                    : string.Format(Strings.ChangeCredential_Success, newEmailAddress);
            }
            else
            {
                TempData["RawErrorMessage"] = string.Format(Strings.ChangeCredential_Failed,
                    HttpUtility.HtmlEncode(newCredential.Identity),
                    UriExtensions.GetExternalUrlAnchorTag("FAQs page", GalleryConstants.FAQLinks.MSALinkedToAnotherAccount));
            }
            return SafeRedirect(returnUrl);
        }
[ HttpGet ] public virtual async Task < ActionResult > LinkOrChangeExternalCredential ( string returnUrl ) { var user = GetCurrentUser ( ) ; var result = await _authService . ReadExternalLoginCredential ( OwinContext ) ; if ( result ? . Credential == null ) { TempData [ " " ] = Strings . ExternalAccountLinkExpired ; return SafeRedirect ( returnUrl ) ; } var newCredential = result . Credential ; if ( await _authService . TryReplaceCredential ( user , newCredential ) ) { await RemovePasswordCredential ( user ) ; var authenticatedUser = await _authService . Authenticate ( newCredential ) ; var usedMultiFactorAuthentication = result . LoginDetails ? . WasMultiFactorAuthenticated ?? false ; await _authService . CreateSessionAsync ( OwinContext , authenticatedUser , usedMultiFactorAuthentication ) ; var newEmailAddress = GetEmailAddressFromExternalLoginResult ( result , out string errorReason ) ; if ( ! string . IsNullOrEmpty ( errorReason ) ) { TempData [ " " ] = errorReason ; return SafeRedirect ( returnUrl ) ; } var linkingDifferentEmailAddress = ! newEmailAddress . Equals ( user . EmailAddress , StringComparison . OrdinalIgnoreCase ) ; TempData [ " " ] = linkingDifferentEmailAddress ? string . Format ( Strings . ChangeCredential_SuccessDifferentEmail , newEmailAddress , user . EmailAddress ) : string . Format ( Strings . ChangeCredential_Success , newEmailAddress ) ; } else { TempData [ " " ] = string . Format ( Strings . ChangeCredential_Failed , HttpUtility . HtmlEncode ( newCredential . Identity ) , UriExtensions . GetExternalUrlAnchorTag ( " " , GalleryConstants . FAQLinks . MSALinkedToAnotherAccount ) ) ; } return SafeRedirect ( returnUrl ) ; }
This is the challenge response action for linking or replacing external credentials
after external authentication.
This is the challenge response action for linking or replacing external credentials after external authentication .
// Remove the password credential after linking to external account. // Authenticate with the new credential after successful replacement // Get email address of the new credential for updating success message // The identity value contains cookie non-compliant characters like `<, >`(eg: John Doe <john@doe.com>),  // These need to be replaced so that they are not treated as HTML tags
None
returnUrl
string
The url to return upon credential replacement
The url to return upon credential replacement