Last active
October 12, 2020 17:01
-
-
Save ghstahl/cdaf12f39a0450729bece55c8bdad106 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IActionResult> OnGetCallbackAsync( | |
string returnUrl = null, | |
string remoteError = null) | |
{ | |
returnUrl = returnUrl ?? Url.Content("~/"); | |
if (remoteError != null) | |
{ | |
ErrorMessage = $"Error from external provider: {remoteError}"; | |
return RedirectToPage("./Login", new {ReturnUrl = returnUrl}); | |
} | |
var info = await _signInManager.GetExternalLoginInfoAsync(); | |
if (info == null) | |
{ | |
ErrorMessage = "Error loading external login information."; | |
return RedirectToPage("./Login", | |
new { ReturnUrl = returnUrl }); | |
} | |
var queryNameId = from claim in info.Principal.Claims | |
where claim.Type == ClaimTypes.NameIdentifier | |
select claim; | |
var nameIdClaim = queryNameId.FirstOrDefault(); | |
await _signInManager.SignOutAsync(); | |
var user = new IdentityUser | |
{ | |
UserName = nameIdClaim.Value, | |
Email = null | |
}; | |
var result = await _userManager.CreateAsync(user); | |
if (result.Succeeded) | |
{ | |
var newUser = await _userManager.FindByIdAsync(user.Id); | |
var eClaims = new List<Claim> | |
{ | |
new Claim(".displayName", user.Id), | |
new Claim(".externalNamedIdentitier",nameIdClaim.Value), | |
new Claim(".loginProvider",info.LoginProvider) | |
}; | |
// normalized id. | |
await _userManager.AddClaimsAsync(newUser, eClaims); | |
await _signInManager.SignInAsync(user, isPersistent: false); | |
await _userManager.DeleteAsync(user); | |
return LocalRedirect(returnUrl); | |
} | |
return RedirectToPage("./Login", new { ReturnUrl = returnUrl }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment