Get original url without non-standard port (C#)

前端 未结 6 885
执念已碎
执念已碎 2021-02-12 21:43

First question!


Environment

MVC, C#, AppHarbor.

Problem

I am calling an openid provider,

相关标签:
6条回答
  • 2021-02-12 21:48

    Something like

    String port = Request.ServerVariables["SERVER_PORT"] == "80" ? "" : ":" + Request.ServerVariables["SERVER_PORT"];
    String virtualRoot = Url.Content("~/");
    destinationUrl = String.Format("http://{0}{1}{2}", Request.ServerVariables["SERVER_NAME"], port + virtualRoot, "/callback");
    
    0 讨论(0)
  • 2021-02-12 21:49

    I see that this is an old thread. I had this issue running MVC5, on IIS 7.5, with an Apache proxy in front. Outside of the server, I would get "Empty Response", since the asp.net app gets the Url from apache with the custom port.

    In order to have the app redirect to a subpath without including the "custom" port, forget the Response/Request objects, and use the Transfer method. For instance, if I want that users are automatically redirected to the login page in case they are not logged already:

    if (!User.Identity.IsAuthenticated)   
        Server.TransferRequest("Account/Login");
    
    0 讨论(0)
  • 2021-02-12 21:58

    This is a common problem in load balanced setups like AppHarbor's - we've provided an example workaround.

    Update: A more desirable solution for many ASP.NET applications may be to set the aspnet:UseHostHeaderForRequestUrl appSetting to true. We (AppHarbor) have seen several customers experience issues using it with their WCF apps, which is why we haven't enabled it by default and stil recommend the above solution for those situations. You can configure it using AppHarbor's "Configuration Variables" to inject the appsettings when deployed. More information can be found in this article.

    0 讨论(0)
  • 2021-02-12 22:04

    If you use the UrlBuilder class in the framework you can easly get around this. On the builder class if you set the port to -1 then the port number will be removed:

    new UriBuilder("http://sub.example.com:15232/callback"){ Port = -1}
    

    returns : http://sub.example.com/callback

    To keep the port number on a local machine just check Request.IsLocal and don't apply -1 to the port.

    I would wrap this into a extension method to keep it clean.

    0 讨论(0)
  • 2021-02-12 22:12

    My initial thoughts are get the referrer variable and check if that includes a port, if so use it otherwise don't.

    If that’s not an option because a proxy might remove the referrer header variable then you might need to use some client side script to get the location and pass it back to the server.

    I'm guessing that AppHarbor use port forwarding to the IIS server so even though publicly the site is on port 80 IIS has it hosted on another port so it can't know what port the client connected on.

    0 讨论(0)
  • 2021-02-12 22:13

    I recently ran into an issue where I compared a URL to the current URL, and then highlighted navigation based on that. It worked locally, but not in production.

    I had http://example.com/path/to/file.aspx as my file, but when viewing that file and running Request.Url.ToString() it produced https://example.com:81/path/to/file.aspx in a load balanced production environment.

    Now I am using Request.Url.AbsolutePath to just give me /path/to/file.aspx, thus ignoring the schema, hostname, and port numbers.

    When I need to compare it to the URL on each navigation item I used: New Uri(theLink.Href).AbsolutePath

    0 讨论(0)
提交回复
热议问题