in Source/WebApp-Service-Provider-DotNet/Controllers/AccountController.cs [161:205]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null)
{
var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
return RedirectToAction(nameof(Login));
}
// acr_values are mapped to this authnclassreference claim by .NET
string acrValues = info.Principal?.FindFirst("http://schemas.microsoft.com/claims/authnclassreference")?.Value;
if (!Validation.IsEIdasLevelMet(acrValues, _config.EIdasLevel))
{
await HttpContext.SignOutAsync(FranceConnectConfiguration.ProviderScheme, new AuthenticationProperties { RedirectUri = Url.Action(nameof(Login), null, null, Request.Scheme) });
throw new UnauthorizedAccessException("Requested EIdas level not met");
}
// Sign in the user with this external login provider if the user already has a login.
var user = await _userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey);
if (user != null)
{
if (await _userManager.IsLockedOutAsync(user))
{
return View("Lockout");
}
await _signInManager.SignInAsync(user, info.AuthenticationProperties, info.LoginProvider);
_logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl ?? Url.Action(nameof(ManageController.PivotIdentity), "Manage"));
}
else
{
// If the user does not have an account, then ask the user to create an account.
ViewData["ReturnUrl"] = returnUrl;
ViewData["LoginProvider"] = info.ProviderDisplayName;
DateTime.TryParseExact(info.Principal.FindFirstValue("birthdate"), "yyyy-MM-dd", new CultureInfo("fr-FR"), DateTimeStyles.AssumeUniversal, out DateTime parsedBirthDate);
ExternalLoginConfirmationViewModel model = new()
{
Email = info.Principal.FindFirstValue("email"),
Gender = info.Principal.FindFirstValue("gender"),
Birthdate = parsedBirthDate,
PreferredName = info.Principal.FindFirstValue("preferred_username"),
GivenName = info.Principal.FindFirstValue("given_name"),
FamilyName = info.Principal.FindFirstValue("family_name")
};
return View("ExternalLoginConfirmation", model);
}
}