Fix Google login callback routing and forwarded headers

This commit is contained in:
Warren Chen 2026-05-05 14:55:12 +09:00
parent 6729f91275
commit fedb011154
2 changed files with 13 additions and 1 deletions

View File

@ -87,7 +87,10 @@ public class AccountController : Controller
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public IActionResult ExternalLogin(string provider, string? returnUrl = null) public IActionResult ExternalLogin(string provider, string? returnUrl = null)
{ {
var redirectUrl = Url.Action(nameof(ExternalLoginCallback), new { returnUrl }); var redirectUrl = Url.Action(
nameof(ExternalLoginCallback),
"Account",
new { area = string.Empty, returnUrl });
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return Challenge(properties, provider); return Challenge(properties, provider);
} }

View File

@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.RateLimiting; using Microsoft.AspNetCore.RateLimiting;
using MemberCenter.Application.Abstractions; using MemberCenter.Application.Abstractions;
using MemberCenter.Application.Constants; using MemberCenter.Application.Constants;
@ -89,6 +90,13 @@ builder.Services.AddAuthorization(options =>
options.AddPolicy("Superuser", policy => policy.RequireRole("superuser")); options.AddPolicy("Superuser", policy => policy.RequireRole("superuser"));
}); });
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
builder.Services.AddRateLimiter(options => builder.Services.AddRateLimiter(options =>
{ {
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests; options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
@ -154,6 +162,7 @@ if (!app.Environment.IsDevelopment())
app.UseHsts(); app.UseHsts();
} }
app.UseForwardedHeaders();
app.UseRouting(); app.UseRouting();
app.UseRateLimiter(); app.UseRateLimiter();
app.UseAuthentication(); app.UseAuthentication();