services.AddCors(options => { options.AddPolicy("AllowMyOrigin", builder => builder.WithOrigins("https://localhost:5001") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials()); });
app.UseCors("AllowMyOrigin");
https://localhost:5001/signin-google
services.AddAuthentication(options => { options.DefaultAuthenticateScheme = GoogleDefaults.AuthenticationScheme; options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme; }) .AddGoogle(options => { options.ClientId = "YourClientId"; options.ClientSecret = "YourClientSecret"; });
Uri currentUri = new Uri("https://localhost:5001/signin-google");
[HttpGet]
[EnableCors("AllowMyOrigin")]
public async Task
通过以上步骤可以解决在 ASP .NET Core 中使用 Google 登录时出现的 CORS 问题。