How to get Cloud Front Distribution IP? - c#

I need to make trust all cloudfront ip addresses in .Net Core Application in ForwardedHeadersOptions to get the Real Client IP Address but not getting that. Using below code to setup ForwardedHeadersOptions in Startup.cs
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("xxx.xx.x.x"), 12));
});
Please let me know, How can get all IPs for cloud front?

Amazon CloudFront has many IP ranges associated with it. Therefore, things might be more complex than you conceive.
You can obtain the IP address ranges of all AWS services at: AWS IP address ranges - AWS General Reference

Related

Google cloud run unable to connect MQTT

Currently I am developing a .Net6 application with a some controllers and a minimal MQTT Server. So far I have everything working correctly on my local machine, however, when deploying to Google Cloud Run (GCR) I am unable to connect to the MQTT Server.
I noticed that the GCR container wants you to map incoming traffic to a single port (8080 in my case), however I am running MQTT on port 1883 (default) and unable to connect to it. The controllers running on port 8080 are accessible and work fine.
I need some direction toward a solution, preferably in a single container.
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseKestrel(o =>
{
o.ListenAnyIP(1883, l => l.UseMqtt());
o.ListenAnyIP(8080);
});
...
var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints
.MapConnectionHandler<MqttConnectionHandler>("/mqtt",
httpConnectionDispatcherOptions =>
httpConnectionDispatcherOptions.WebSockets.SubProtocolSelector = protocolList => protocolList.FirstOrDefault() ?? string.Empty);
});
app.UseMqttServer(server => server.StartAsync());
app.MapControllers();
app.Run();
A possible option is to not use 2 ports.
If you use MQTT over WebSockets then your broker can share a port with the HTTP server.

.Net 6 Web API returning a wrong remote IP address through Apache proxy

I have a .Net 6 C# web API that needs to verify the client IP address in some controller methods to reinforce the security. The self-contained published application run as a local service on Linux and is accessible through Apache which is used as a reverse proxy.
In my controller methods I use this line to get the IP address:
string currentIp = _httpContext.Connection.RemoteIpAddress.ToString();
In the program.cs file I added this:
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
And finally in my Apache configuration file there is:
<VirtualHost *:443>
ProxyPreserveHost On
ProxyPass "/api/" http://127.0.0.1:5000/
ProxyPassReverse "/api/" http://127.0.0.1:5000/
...
The problem is that the value of the remote IP address (currentIp variable) in my controller is always the same, something like "165.30.0.1" which is a device (a router ?) on the server network "165.30.0.0" I guess. The server address on this network is "165.30.0.100" through a VPN by the way.
I have already tried the following in the C# "builder.Services.Configure" above:
options.ForwardLimit = null; // Or = 2
options.KnownProxies.Clear(); // Or .Add() with some IP addresses
options.KnownNetwork.Clear(); // Or .Add() with some IP addresses
I have also tested the Apache configuration file above with:
RequestHeader set "X-Forwarded-For" expr=%{REMOTE_ADDR}
But always this damn "165.30.0.1" IP address is returned and never the client's real IP address.
The server is controlled by a contractor so could it be a router or firewall protection that generates this behavior or is it caused by my configuration ?

Access ASP.NET Core 2.1 web app hosted on localhost from mobile device

I create an out of the box asp.net core 2.1 web app with react.
When I run it, it hosts on localhost:(some port).
I want to access that on my mobile device.
I tried accessing it from localhost by adding the port to the Inbound Rules on Windows Defender Firewall.It didn't work so I wanted to change the ip address to 0.0.0.0:(some port) or to host it on my local IPv4 address by changing the lauchSettings.json and Program.cs by .UseUrls method. It didn't work, gave me an error that the app cannot run.
I tried using my external IP address which I got from asking "My ip address" in Google but that didn't help as well.
I use this, maybe it can help
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("https://*:5566")
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
Note that for the latest .Net, you set the URLs in the launchSettings.json file as shown below:
Make sure to enable the port 5566 in firewall. From the phone access https://your_comp_ip_address:5566/ it should work.
Change your_comp_ip_address to your computer ip address, you can check it from the CMD. Just run 'ipconfig' in the CMD
From Visual Studio, run the App itself, not IIS Express, select AppNamefrom the drop down, then press F5. Kindly Check all of these photos below:
localhost always points to the current device loopback address, so localhost on your pc points to itself and localhost on a phone points to itself as well. If your phone is connected to the same network as your pc you could launch the site on a specific local ip address instead of localhost and connect using the ipaddress. But I don't think it is possible to use IISExpress with anything other than localhost. You could use a different launch profile and just use kestrel directly with an ipaddress instead of using IISExpress.
If you use an android emulator on your machine then you could access the site via a special ipaddress that allows the emulator to connect to the pc, so if running at localhost:5566, from the emulator you would use http://10.0.2.2:5566
I'm not sure about other emulators for iphone etc, they may have different conventions.
In most cases, all you will have to do is replace "localhost" with a wildcard * (or with your local LAN IPv4 Address) in Properties/launchSettings.json:
{
// ...
"profiles": {
// ...
"Moshoboga": {
// ...
// replace: "applicationUrl": "https://localhost:5001;http://localhost:5000"
"applicationUrl": "https://*:5001;http://*:5000"
}
}
}
Then navigate to your local ip in any browser on your LAN. Find this with ipconfig on Windows, or ifconfig on Unix/Linux
For newer .NET Core (v6) just add your your IP url in applicationUrl property in launchSettings.json file like that:
"applicationUrl":https://localhost:7001;http://localhost:7000;http://192.168.1.78:7000;https://192.168.1.78:7001"
You can find out your current local IP address (IPv4) in cmd/powershell (Windows) by typing ipconfig

How do I get the local host IP address in a Windows Store app?

A particular rest service I am calling in a Windows Store app wants the IP address of the calling computer as a parameter. None of the tried and true examples using System.Net or System.Net.NetworkInformation compile in a Store app.
What is the magical combination of types and methods available in a windows store app that will get to the local machine's IP address? I feel like it is probably obvious but I am not seeing it!
You will have to contact an external server. Even if the platform supplies an API to retrieve the network address, the host could still be located behind a proxy or a NAT (and you will see something like 192.168.1.4, instead of your external IP address).
Just perform an HTTP request towards services like http://ifconfig.me/ or http://whatismyip.com/ and parse the IP.
Local network IP address:
foreach (HostName localHostName in NetworkInformation.GetHostNames())
{
if (localHostName.IPInformation != null)
{
if (localHostName.Type == HostNameType.Ipv4)
{
// E.g.: 192.168.1.108
Debug.WriteLine(localHostName);
}
}
}

How can i get IP Address of my 3G modem?

My GPRS modem has got a sim card. it can connect Web. Web service give it a ip number. i need it. like that: http://www.your-ip-address.com/
How can i do that C#?
You can use the static method WebClient.DownloadString(url) to read your external IP address from any web service providing such data:
string ip = System.Net.WebClient.DownloadString("http://whatismyip.org/");
If you are going to use this in a production environment, better make sure that the URL you are pointing to, is guaranteed to stay around for the entire lifespan of your application. The best way is probably to host the web service yourself.
Also, you should add some error checking around this code, as it will fail if the internet connection or the web service is unavailable.
You can get a list of your IP addresses via DNS using the following code:
var name = Dns.GetHostName();
var entry = Dns.GetHostEntry(name);
foreach (var address in entry.AddressList) {
Console.WriteLine(address);
}
If you want the IP address as a property of the hardware, you can use the System.Management.ManagementClass with the name Win32_NetworkAdapterConfiguration. See http://msdn.microsoft.com/en-us/library/system.management.managementclass.aspx for details.
You can create a WebRequest to http://whatismyip.com/automation/n09230945.asp which houses only your IP address
Start here

Categories

Resources