I’m creating my app with an ASP.NET Web API + React. And I can’t do Google OAuth 2.0. Can someone point to a guide, or explain how I need to do it?
I tried this, but I don’t understand how I need to link it with React.
Program.cs
:
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
//.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
ClockSkew = TimeSpan.Zero
};
})
.AddGoogle(options =>
{
options.ClientId = builder.Configuration["Google:ClientId"];
options.ClientSecret = builder.Configuration["Google:ClientSecret"];
options.CallbackPath = "/api/Authorization/GoogleResponse";
})
.AddFacebook(options =>
{
options.AppId = builder.Configuration["Facebook:AppId"];
options.AppSecret = builder.Configuration["Facebook:AppSecret"];
options.CallbackPath = "/api/authorization/FacebookCallback";
});
Authorization controller:
[HttpGet("LoginGoogle")]
public IActionResult Login(string returnUrl = "/")
{
//var properties = new AuthenticationProperties { RedirectUri = returnUrl };
//return Challenge(properties, "Google");
string redirecturl = Url.ActionLink("GoogleResponse", "Authorization");
var properties = signInManager.ConfigureExternalAuthenticationProperties("Google", redirecturl);
var chall = new ChallengeResult("Google", properties);
return chall;
}
[HttpGet("GoogleResponse")]
public async Task<IActionResult> GoogleResponse()
{
var authenticateResult = await HttpContext.AuthenticateAsync("Google");
if (!authenticateResult.Succeeded)
{
return BadRequest(new Error( "Failed to authenticate with Google" ));
}
var user = authenticateResult.Principal;
authentication.LoginOrCreateUser(user);
var token = authentication.CreateToken();
return Ok(token);
}