I am trying to implement the login with google functionality in asp.net core web api using identity external provider. All things are going well but when my url is redirecting to this external-auth-callback function I am getting info null. This is the external-auth-callback function.
This is my program.cs file where i have write authentication code
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddCookie()
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = builder.Configuration["Jwt:ValidIssuer"],
ValidAudience = builder.Configuration["Jwt:ValidAudience"],
IssuerSigningKey = new SymmetricSecurityKey
(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Secret"])),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = false,
ValidateIssuerSigningKey = true
};
}).AddGoogle(opt =>
{
opt.ClientId = builder.Configuration["GoogleLoginProvider:ClientId"];
opt.ClientSecret = builder.Configuration["GoogleLoginProvider:ClientSecret"];
opt.SignInScheme = IdentityConstants.ExternalScheme;
});
here is my external-login function
public async Task ExternalLogin(string provider, string returnUrl) { var redirectUrl = $"https://localhost:7008/api/account/external-auth-callback?returnUrl={returnUrl}"; var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); properties.AllowRefresh = true;
after that I am redirecting to my frontend url
const handleExternalLogin = async () => { const url = ${externalLogin}?provider=Google&returnUrl=/admin-dashboard; const response = await getData(url); if (response.isSuccessfull) { const redirectUrl = response.data.properties.items[".redirect"]; const loginProvider = response.data.properties.items["LoginProvider"]; const googleAuthorizationUrl = https://accounts.google.com/o/oauth2/v2/auth + ?client_id=xxxxx.apps.googleusercontent.com + &redirect_uri=${redirectUrl} + &response_type=code + &scope=openid%20profile%20email + &state=${loginProvider}; window.location.href = googleAuthorizationUrl; } };
and then redirecting to google sign-in page. It will redirects to that page but when i choose my account it will go to backend external auth callback function. Given is external-auth callback function. But when i get to this method I get the info null but here i need to get the user details who coming from external login and don't understand why it is happing
var info = await _signInManager.GetExternalLoginInfoAsync();
if (info != null)
{
var signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider,
info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
return new MessageViewModel()
{
IsSuccess = signInResult.Succeeded,
Message = "User with this account is already in table",
};
}
else
{
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
user = new Users
{
UserName = info.Principal.FindFirstValue(ClaimTypes.Email),
Email = info.Principal.FindFirstValue(ClaimTypes.Email),
FirstName = info.Principal.FindFirstValue(ClaimTypes.GivenName),
LastName = info.Principal.FindFirstValue(ClaimTypes.Surname),
};
await _userManager.CreateAsync(user);
}
await _userManager.AddLoginAsync(user, info);
await _signInManager.SignInAsync(user, isPersistent: false);
return new MessageViewModel()
{
IsSuccess = true,
Message = "User with this account is created successfully"
};
}
}