Skip to content

AddProxySupport on .NET Framework not working with UseForwardedHeaders #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gumbarros opened this issue Aug 26, 2022 · 4 comments · Fixed by #175
Closed

AddProxySupport on .NET Framework not working with UseForwardedHeaders #174

gumbarros opened this issue Aug 26, 2022 · 4 comments · Fixed by #175
Milestone

Comments

@gumbarros
Copy link
Contributor

gumbarros commented Aug 26, 2022

Describe the bug

As described in this issue , UseForwardedHeaders is not working, my website is displaying the subroute instead of the original request. I don't know if this is a SystemWebAdapter issue or YARP issue, please close this issue if it is 100% YARP related.

To Reproduce

Host in IIS a .NET Core website on www.site.com
Host in IIS a .NET Framework website on www.site.com/subapplication
Create a simple /Login route on .NET Framework website.
Add to .NET Framework website global.asax.vb:

   Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        RegisterSystemWebAdapters()
        
        Log.AddInfo("Sistema Dansales Web Iniciado...")
        
        AreaRegistration.RegisterAllAreas()
        RegisterRoutes(RouteTable.Routes)
        RegisterGlobalFilters(GlobalFilters.Filters)

        AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimsIdentity.DefaultNameClaimType
    End Sub

    Private Sub RegisterSystemWebAdapters()
        AddSystemWebAdapters() _
        .AddProxySupport(Sub(options)
                                               options.UseForwardedHeaders = True
                                       End Sub)
     End Sub

Go to www.site.com

Expected:
www.site.com/Login

Actual:
www.site.com/subapplication/Login

Technical Info

ASP.NET Framework Application:

  • Technologies and versions used: MVC + WebForms
  • .NET Framework Version: 4.8
  • Windows Version: Server 2012

ASP.NET Core Application:

  • Targeted .NET version: 6.0
  • .NET SDK version: 6.0

Why this is a migration problem

Because we have 100+ references using this getter:

        /// <summary>
        /// Captura a URL base do sistema
        /// </summary>
        /// <returns>URL</returns>
        /// <remarks>Lucio Pelinson 11-02-2009 / Gustavo Barros 24-08-2022</remarks>
        public static string PATH
        {
            get
            {
                var request = HttpContext.Current.Request;
                var helper = new UrlHelper(request.RequestContext);
                return string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, helper.Content("~"));
            }
        }

This causes many hyperlinks created with this getter to wrongly redirect only to the .NET Framework site instead of the Core one, but it don't happen when we use www.site.com/Login, only when we use www.site.com/subapplication/Login.

@twsouthwick
Copy link
Member

Yes, the virtual path needs to be the same for this set up to work. Otherwise things like routing just don't work well. I'm happy to hear if someone is able to make it work, but I've gone down that rabbit hole and am fairly convinced that the path of the application needs to be the same.

I'm going to leave this open so we can ensure this is documented.

@gumbarros
Copy link
Contributor Author

@twsouthwick thanks for the response! Is this the recommended structure? When I open /devCore, my site is still displaying /dev 😢

image

@twsouthwick
Copy link
Member

twsouthwick commented Aug 26, 2022

@gumbarros I'm not an IIS expert, so I may not be interpreting that screenshot fully - please correct me if my understanding here is wrong.

It appears that you have dev and devCore as separate applications within the same site. Instead, you'll want to set it up so that they are on different sites. They can still have the same virtual directory/application structure, but the names of those must match.

@gumbarros
Copy link
Contributor Author

gumbarros commented Aug 26, 2022

@twsouthwick thank you very much! Solved my problem. For anyone with difficulties:
image


Just set the two websites with the same folder name.
The port 88 is the .NET Framework one, it's being hosted as localhost only. Edited appsettings.json to:

  "ReverseProxy": {
    "Routes": {
      "fallbackRoute": {
        "ClusterId": "fallbackCluster",
        "Order": "1",
        "Match": {
          "Path": "{**catch-all}"
        }
      }
    },
    "Clusters": {
      "fallbackCluster": {
        "Destinations": {
          "fallbackApp": {
            "Address": "http://localhost:8088/dev/"
          }
        }
      }
    }
  }

.NET Core app now reverse proxy to the .NET Framework localhost.
Also edited PATH getter to prevent localhost being displayed in hrefs and CSS files to the client.

        /// <summary>
        /// Captura a URL base do sistema
        /// </summary>
        /// <returns>URL</returns>
        /// <remarks>Lucio Pelinson 11-02-2009 / Gustavo Barros 24-08-2022</remarks>
        public static string PATH
        {
            get
            {
                var request = HttpContext.Current.Request;
                var helper = new UrlHelper(request.RequestContext);
                return helper.Content("~");
            }
        }

Congratulations to everyone on this project, everything working like a charm.

@danroth27 danroth27 added this to the 1.0 RC1 milestone Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants