.net 每次在ECS中部署应用程序(使用Fargate)时,都提示用户重新通过Google(使用OIDC)进行身份验证

yftpprvb  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(131)

我有一个使用AWS ECS Fargate托管的.Net 7 Web应用程序(在2个任务上运行)。我使用Google作为OIDC提供商,并且能够成功地对用户进行身份验证(本地和生产)。
我使用以下代码来实现这一点:

  1. builder.Services.AddAuthentication(options =>
  2. {
  3. options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  4. options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
  5. })
  6. .AddCookie(options =>
  7. {
  8. options.Cookie.Name = "AppAuth";
  9. options.ExpireTimeSpan = TimeSpan.FromHours(12);
  10. options.SlidingExpiration = false;
  11. options.Events = new CookieAuthenticationEvents
  12. {
  13. OnValidatePrincipal = async context =>
  14. {
  15. var claimsPrincipal = context.Principal;
  16. var email = claimsPrincipal.FindFirstValue(ClaimTypes.Email);
  17. if (claimsPrincipal != null)
  18. {
  19. var claimsIdentity = (ClaimsIdentity)claimsPrincipal.Identity;
  20. if (claimsIdentity != null)
  21. {
  22. if (!claimsIdentity.HasClaim(x => x.Type == "user-id"))
  23. {
  24. var response = await api.GetUsersAsync(email: email);
  25. claimsIdentity.AddClaim(new Claim("user-id", response.Users.Single().UserId.ToString()));
  26. }
  27. }
  28. await context.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, context.Properties);
  29. }
  30. }
  31. };
  32. })
  33. .AddOpenIdConnect(options =>
  34. {
  35. options.Authority = "https://accounts.google.com";
  36. options.ClientId = "***";
  37. options.ClientSecret = "***";
  38. options.ResponseType = OpenIdConnectResponseType.Code;
  39. options.Scope.Add("openid");
  40. options.Scope.Add("email");
  41. options.GetClaimsFromUserInfoEndpoint = true;
  42. options.CallbackPath = "/oauth2/idpresponse";
  43. options.Events = new OpenIdConnectEvents
  44. {
  45. OnRedirectToIdentityProvider = context =>
  46. {
  47. if (!builder.Environment.IsDevelopment())
  48. {
  49. var uriBuilder = new UriBuilder(context.ProtocolMessage.RedirectUri)
  50. {
  51. Scheme = "https",
  52. Port = -1
  53. };
  54. context.ProtocolMessage.RedirectUri = uriBuilder.ToString();
  55. }
  56. return Task.FromResult(0);
  57. }
  58. };
  59. });

字符串
我遇到的问题是,每次我使用AWS ECS Fargate部署应用程序,然后刷新页面时,它都会将我重定向回Google登录页面。为了清楚起见,在Fargate上部署应用程序的过程包括启动两个新任务,然后销毁现有任务。
另一件值得一提的事情(可能相关)是,我打开了粘性会话(需要启用Redis背板才能让SignalR工作,see here)。
另一件可能值得一提的事情是我不能在本地复制它;当我重新启动我的应用程序,然后刷新页面,它保持登录的经验。

sqxo8psd

sqxo8psd1#

这可能是因为您的网站的cookie加密密钥在每次部署时都会重新生成为新值。因此,任何留在浏览器中的cookie一旦被新部署接收就无法解密,因此.NET触发新的登录重定向。
您应该能够通过使用浏览器工具查看请求和响应详细信息来验证是否发生了这种情况。如果使用Chrome,请使用preserve log option,以避免在Google重定向时丢失旧请求。
如果这确实是原因,解决方案是使用data protection options之一,例如将密钥持久化到redis或数据库,以确保在重新部署应用时使用相同的密钥。另请注意,默认情况下,密钥每90天回收一次,这将导致相同的cookie拒绝事件。

相关问题