ASP.NET MVC站点在AWS ELB后面无法正常工作

时间:2016-10-28 14:19:37

标签: asp.net asp.net-mvc amazon-web-services amazon-elb

我尝试将使用单个用户帐户的ASP.NET MVC Web应用程序部署到使用弹性负载均衡器(ELB)的AWS服务器。我已将该站点部署到AWS应用服务器上的IIS,并将其连接到我的AWS SQL服务器,并且它在服务器上按预期工作(实际上当我在Visual Studio中运行它或部署到内部服务器时)。

当远程访问它时会出现问题,当然这是通过ELB进行的。

所以基本上,如果我已经登录,那很好。如果我没有登录,登录页面就可以了,但没有别的。我的想法,以及我们与AWS合作的内部团队的同事(我无法帮助我,我已经问过了!)是当我被重定向到登录页面时,它是&#39 ;是HTTP请求而非HTTPS,这是问题的原因,但无论我尝试过什么,我都无法通过HTTPS重定向。我试过了:

  • 在我的web.config文件中添加规则以获取转发的请求并将其重定向到HTTPS - 这似乎没有产生明显的差异
  • 添加到我的FilterConfig或Login操作
  • 的各种不同属性
  • 使用URL Rewrite
  • 直接在IIS中添加规则

显然我的解决方法是让每个人都去登录页面并从那里开始,而不仅仅是根URL,但我真的很想把它排序好像重定向不在这里工作,我可以看到它在其他地方没有工作并可能导致问题。

按要求更新:我实际上无法控制我的ELB,因为这是由不同团队完成的,但我对团队说话的理解是它接受流量作为HTTPS然后通过它作为HTTP到服务器上。

1 个答案:

答案 0 :(得分:3)

您的MVC应用程序配置为在用户需要登录时重定向到绝对http网址而不是相对网址。

对于基于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  

然后,在课程的后面添加以下功能:

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。