ASP.NET MVC 站点在 AWS ELB 后无法正常工作
ASP.NET MVC site not working correctly behind AWS ELB
我正在尝试将使用单个用户帐户的 ASP.NET MVC Web 应用程序部署到使用弹性负载平衡器 (ELB) 的 AWS 服务器。我已经将该站点部署到 AWS 应用程序服务器上的 IIS,将其连接到我的 AWS SQL 服务器,并且它在服务器上按预期工作(实际上当我 运行 它在 Visual Studio 或部署到内部服务器)。
当然是通过ELB远程访问,问题就来了。
- 如果我尝试访问 https://www.example.com/ it doesn't work - it will redirect me to http://www.example.com/Account/Login?ReturnUrl=%2F,它会挂起,然后出现 408 错误。
- 如果我尝试访问,比方说,https://www.example.com/Dashboard/Index it doesn't work - it will redirect me to http://www.example.com/Account/Login?ReturnUrl=%2FDashboard%2FIndex,然后我得到了 408 错误。
- 如果我尝试访问 https://www.example.com/Account/Login directly, it works fine. I can then log in and all pages in my site work as expected. If I log out though, I get redirected to http://www.example.com/Account/Login?ReturnUrl=%2F,我会得到一个 408 错误。
所以基本上,如果我登录了,就没问题了。如果我没有登录,登录页面是好的,但没有别的。我的想法,以及我们内部团队的一位与 AWS 合作的同事(顺便说一句,他无法帮助我,我已经问过!)是,当我被重定向到登录页面时,它是一个 HTTP 请求而不是 HTTPS ,这就是问题的原因,但无论我尝试了什么,我都无法使用 HTTPS 进行重定向。我试过:
- 在我的 web.config 文件中添加规则以获取转发的请求并将它们重定向到 HTTPS - 这似乎没有任何明显的区别
- 添加到我的 FilterConfig 或登录操作的各种不同属性
- 使用 URL Rewrite
直接在 IIS 中添加规则
显然,我的解决方法是让每个人都转到登录页面并从那里开始,而不仅仅是根 URL,但我真的很想对此进行排序,就好像重定向在这里不起作用一样,我可以看到它在其他地方不起作用并可能导致问题。
按要求更新:我实际上对我的 ELB 没有任何控制权,因为这是由另一个团队完成的,但我从与团队交谈中了解到,它接受 HTTPS 流量,然后将其传递给服务器作为 HTTP。
您的 MVC 应用程序配置为在用户需要登录时重定向到绝对 http URL 而不是相对 URL。
对于基于 Owin 中间件的新 MVC 应用程序,这在 App_Start/Startup.Auth.cs
中配置。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
并在 OnValidateIdentity
属性 之后添加以下内容:
OnApplyRedirect = ApplyRedirect
然后,稍后在class中添加以下函数:
private static void ApplyRedirect(CookieApplyRedirectContext context)
{
Uri absoluteUri;
if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, out absoluteUri))
{
context.Response.Redirect(absoluteUri.PathAndQuery);
return;
}
context.Response.Redirect(context.RedirectUri);
}
基本上,这是将绝对 URL 转换为相对 URL。然后将相对 URL 传递回浏览器。由于重定向是相对的,它应该保持 https URL.
我正在尝试将使用单个用户帐户的 ASP.NET MVC Web 应用程序部署到使用弹性负载平衡器 (ELB) 的 AWS 服务器。我已经将该站点部署到 AWS 应用程序服务器上的 IIS,将其连接到我的 AWS SQL 服务器,并且它在服务器上按预期工作(实际上当我 运行 它在 Visual Studio 或部署到内部服务器)。
当然是通过ELB远程访问,问题就来了。
- 如果我尝试访问 https://www.example.com/ it doesn't work - it will redirect me to http://www.example.com/Account/Login?ReturnUrl=%2F,它会挂起,然后出现 408 错误。
- 如果我尝试访问,比方说,https://www.example.com/Dashboard/Index it doesn't work - it will redirect me to http://www.example.com/Account/Login?ReturnUrl=%2FDashboard%2FIndex,然后我得到了 408 错误。
- 如果我尝试访问 https://www.example.com/Account/Login directly, it works fine. I can then log in and all pages in my site work as expected. If I log out though, I get redirected to http://www.example.com/Account/Login?ReturnUrl=%2F,我会得到一个 408 错误。
所以基本上,如果我登录了,就没问题了。如果我没有登录,登录页面是好的,但没有别的。我的想法,以及我们内部团队的一位与 AWS 合作的同事(顺便说一句,他无法帮助我,我已经问过!)是,当我被重定向到登录页面时,它是一个 HTTP 请求而不是 HTTPS ,这就是问题的原因,但无论我尝试了什么,我都无法使用 HTTPS 进行重定向。我试过:
- 在我的 web.config 文件中添加规则以获取转发的请求并将它们重定向到 HTTPS - 这似乎没有任何明显的区别
- 添加到我的 FilterConfig 或登录操作的各种不同属性
- 使用 URL Rewrite 直接在 IIS 中添加规则
显然,我的解决方法是让每个人都转到登录页面并从那里开始,而不仅仅是根 URL,但我真的很想对此进行排序,就好像重定向在这里不起作用一样,我可以看到它在其他地方不起作用并可能导致问题。
按要求更新:我实际上对我的 ELB 没有任何控制权,因为这是由另一个团队完成的,但我从与团队交谈中了解到,它接受 HTTPS 流量,然后将其传递给服务器作为 HTTP。
您的 MVC 应用程序配置为在用户需要登录时重定向到绝对 http URL 而不是相对 URL。
对于基于 Owin 中间件的新 MVC 应用程序,这在 App_Start/Startup.Auth.cs
中配置。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
并在 OnValidateIdentity
属性 之后添加以下内容:
OnApplyRedirect = ApplyRedirect
然后,稍后在class中添加以下函数:
private static void ApplyRedirect(CookieApplyRedirectContext context)
{
Uri absoluteUri;
if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, out absoluteUri))
{
context.Response.Redirect(absoluteUri.PathAndQuery);
return;
}
context.Response.Redirect(context.RedirectUri);
}
基本上,这是将绝对 URL 转换为相对 URL。然后将相对 URL 传递回浏览器。由于重定向是相对的,它应该保持 https URL.