updated OnGetExternalLoginCallbackAsync with the code to update name, surname and phone number when user logs in. It is working ok and able to update the local db.
public override async Task<Microsoft.AspNetCore.Mvc.IActionResult> OnGetExternalLoginCallbackAsync(string returnUrl = "", string returnUrlHash = "", string remoteError = null)
{
var loginInfo = await SignInManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToPage("./Login");
}
var result = await SignInManager.ExternalLoginSignInAsync(
loginInfo.LoginProvider,
loginInfo.ProviderKey,
isPersistent: false,
bypassTwoFactor: true
);
if (!result.Succeeded)
{
await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
{
Identity = IdentitySecurityLogIdentityConsts.IdentityExternal,
Action = "Login" + result
});
}
if (result.IsLockedOut)
{
throw new UserFriendlyException("Cannot proceed because user is locked out!");
}
if (result.IsNotAllowed)
{
throw new UserFriendlyException("Cannot proceed because user is not allowed!");
}
if (result.Succeeded)
{
var user = await UserManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey);
user.Name = loginInfo.Principal.FindFirstValue(AbpClaimTypes.Name);
user.Surname = loginInfo.Principal.FindFirstValue(AbpClaimTypes.SurName);
var phoneNumber = loginInfo.Principal.FindFirstValue(AbpClaimTypes.PhoneNumber);
if (!phoneNumber.IsNullOrWhiteSpace())
{
var phoneNumberConfirmed = string.Equals(loginInfo.Principal.FindFirstValue(AbpClaimTypes.PhoneNumberVerified), "true", StringComparison.InvariantCultureIgnoreCase);
user.SetPhoneNumber(phoneNumber, phoneNumberConfirmed);
}
await UserManager.UpdateAsync(user);
if (IsLinkLogin)
{
using (CurrentPrincipalAccessor.Change(await SignInManager.CreateUserPrincipalAsync(user)))
{
await IdentityLinkUserAppService.LinkAsync(new LinkUserInput
{
UserId = LinkUserId.Value,
TenantId = LinkTenantId,
Token = LinkToken
});
}
}
await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext
{
Identity = IdentitySecurityLogIdentityConsts.IdentityExternal,
Action = result.ToIdentitySecurityLogAction(),
UserName = user.UserName
});
return RedirectSafely(returnUrl, returnUrlHash);
}
return RedirectSafely(returnUrl, returnUrlHash);
}
public override async Task<Microsoft.AspNetCore.Mvc.IActionResult> OnGetExternalLoginCallbackAsync(string returnUrl = "", string returnUrlHash = "", string remoteError = null)
{
var loginInfo = await SignInManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToPage("./Login");
}
var result = await SignInManager.ExternalLoginSignInAsync(
loginInfo.LoginProvider,
loginInfo.ProviderKey,
isPersistent: false,
bypassTwoFactor: true
);
var claims = loginInfo.Principal.Claims;
if (!result.Succeeded)
{
await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext()
{
Identity = IdentitySecurityLogIdentityConsts.IdentityExternal,
Action = "Login" + result
});
}
if (result.IsLockedOut)
{
throw new UserFriendlyException("Cannot proceed because user is locked out!");
}
if (result.IsNotAllowed)
{
throw new UserFriendlyException("Cannot proceed because user is not allowed!");
}
if (result.Succeeded)
{
var user = await UserManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey);
if (IsLinkLogin)
{
using (CurrentPrincipalAccessor.Change(await SignInManager.CreateUserPrincipalAsync(user)))
{
await IdentityLinkUserAppService.LinkAsync(new LinkUserInput
{
UserId = LinkUserId.Value,
TenantId = LinkTenantId,
Token = LinkToken
});
}
}
await IdentitySecurityLogManager.SaveAsync(new IdentitySecurityLogContext
{
Identity = IdentitySecurityLogIdentityConsts.IdentityExternal,
Action = result.ToIdentitySecurityLogAction(),
UserName = user.UserName
});
return RedirectSafely(returnUrl, returnUrlHash);
}
return RedirectSafely(returnUrl, returnUrlHash);
}
Thanks. Was able to override the OnGetExternalLoginCallbackAsync. still can't get the personal info updated for already existing users though all the claims are being retrieved properly.
I don't have .Web Project in my solution. Where do I override OnGetExternalLoginCallbackAsync method?
ok Thanks. If I update say the phone number on Azure, I am not able to get the updated phone number into Personal Info.
I am signing in with new user. Also I have noted that if I update any information on Personal Info, it is not updating on to Azure side of things.
I am using OpenIdConnect and UserInfo endpoint custom policy for authentication. After successful authentication from Azure, I got the phoneNumber into the token. In Personal Info tab, I am getting User name, Name, Surname and Email but not the Phone Number.
I am getting below claims into the token. Not sure what I am missing to get phone number value in the textbox.