I have a backend on ASP.NET Core Web API and frontend on Angular 9. I am implementing google authentication. In postman It works fine, but in my angular app when I try to authorize via google I get error:
Access to XMLHttpRequest at 'https://accounts.google.com/o/oauth2/v2/...
(redirected from 'https://localhost:44340/api/auth/googlelogin')
from origin 'http://localhost:4200' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
Redirect is not allowed for a preflight request.
CORS configuration on backend:
services.AddCors(options =>
{
options.AddPolicy("MyPolicy",
builder =>
{
builder
.WithOrigins(Configuration["ApplicationSettings:Client_URL"].ToString())
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
app.UseCors("MyPolicy");
Method in my service
public ChallengeResult LoginViaGoogle()
{
var provider = "Google";
var redirectUrl = "/api/auth/ExternalLoginCallBack";
var properties = Database.SignInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}
Method in my controller
[HttpGet("GoogleLogin")]
public IActionResult GoogleLogin()
{
ChallengeResult result;
try
{
result = _authService.LoginViaGoogle();
}
catch(Exception ex)
{
throw ex;
}
return result;
}
My method in Angular:
googleLogin(){
const httpOptions = {
headers: new HttpHeaders({
"Access-Control-Allow-Origin": "*"
})
};
return this.http.get("https://localhost:44340/api/auth/googlelogin", httpOptions);
}
I have tried a lot of solutions from this website, but none helped.
I think that the problem may be that I incorrectly registered the URL in google oauth.

http://localhost:4200, everything works fine except google authentication.https://localhost:44340/api/auth/googleloginin browser or in postman I can authorize and get token, so I think problem is not connected to localhost.