2
var displayUrl = UriHelper.GetDisplayUrl(Request);
var urlBuilder = new UriBuilder(displayUrl) {  Query = null, Fragment = null };

string _activation_url = urlBuilder.ToString().Substring(0, urlBuilder.ToString().LastIndexOf("/")) +"/this_is_my_link.html";

I expect to get correct uri production path, but I still get

http://localhost:5000/api/mdc/this_is_my_link.html

I deployed this on centos 7

please help me..

Thanks

Don

Donald
  • 551
  • 2
  • 6
  • 22
  • Do you have some kind of reverse proxy in front of it? If so, does the reverse proxy send headers such as `X-Forwarded-For`, `X-Forwarded-Host` and `X-Forwarded-Proto`? Finally, have you configured your application to accept these (and to trust the reverse proxy)? – ProgrammingLlama Apr 16 '19 at 04:31

2 Answers2

0

on asp core use

absoluteUri = string.Concat(
                           request.Scheme,
                           "://",
                           request.Host.ToUriComponent(),
                           request.PathBase.ToUriComponent(),
                           request.Path.ToUriComponent(),
                           request.QueryString.ToUriComponent());

or you may choose either Getting absolute URLs using ASP.NET Core

Lapenkov Vladimir
  • 3,066
  • 5
  • 26
  • 37
  • Can you explain a little about how this solves the problem? I'm not convinced it's any different, especially not when the [implementation of the method OP is using](https://github.com/aspnet/HttpAbstractions/blob/187e89f6f0ee67728a50f987d63234c1900130fd/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs#L196-L217) takes the `Host` value from the same place as your proposed solution does. – ProgrammingLlama Apr 16 '19 at 05:00
  • if you use https://github.com/aspnet/HttpAbstractions/blob/187e89f6f0ee67728a50f987d63234c1900130fd/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs#L196-L217. use public static string GetEncodedUrl(this HttpRequest request) – Lapenkov Vladimir Apr 16 '19 at 06:01
  • My point is that your method and OPs method all use `request.Host`, so OP will get `localhost` from your method as well as those methods. – ProgrammingLlama Apr 16 '19 at 06:02
  • If request.Host always returns localhost, this may be unproper network settings, i' would recommend you to create issue on https://github.com/aspnet/AspNetCore – Lapenkov Vladimir Apr 16 '19 at 06:17
  • It seems unlikely to be a bug. A more likely scenario is improper configuration when using ASP.NET Core with a reverse proxy in front of it. If you don't set up ASP.NET Core to accept the headers provided by the reverse proxy, it will look like all requests originate from the reverse proxy (and if it's on the same machine, the reverse proxy likely connects to the API as localhost), rather than the actual clients. I'll provide an answer to that effect if OP confirms that this is the case :) – ProgrammingLlama Apr 16 '19 at 06:24
  • you need to use IHttpContextAccessor to access the context and get the requests stated above if you're outside the controller _httpContextAccessor.HttpContext.Request; – Yzak Jun 02 '21 at 08:38
0

If you are using a reverse proxy, you should read this guide from Microsoft.

Essentially, your reverse proxy should provide these headers to your ASP.NET Core Application:

  • X-Forwarded-For - The client IP
  • X-Forwarded-Host - The Host header from the client (e.g. www.example.com:80)
  • X-Forwarded-Proto - The protocl (e.g. HTTPS)

Then you need to configure your ASP.NET Core application to accept them. You can do so by calling the app.UseForwardedHeaders() method in your Startup's Configure method.

By default (if I'm reading the docs correctly) UseForwardedHeaders (called as above) will accept X-Forwarded-For and X-Forwarded-Proto from a localhost reverse proxy.

If your situation is more complicated than that, you must configure the headers you want/the trusted reverse proxies:

var forwardedOptions = new ForwardedHeadersOptions()
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto // allow for, host, and proto (ForwardedHeaders.All also works here)
};

// if it's a single IP or a set of IPs but not a whole subnet
forwardedOptions.KnownProxies.Add(IPAddress.Parse("192.168.0.5"));

// if it's a whole subnet
forwardedOptions.KnownNetworks.Add(new IPNetwork("192.168.0.1", 24)); // 192.168.0.1 - 192.168.0.254

app.UseForwardedHeaders(forwardedOptions);

Also note that, depending on the reverse proxy you use, you might need to configure this on the reverse proxy

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86