0

I am accessing a token generator that generates a token based on my IP (and my username & password). Since my code is running on App Service on Azure, it might use any of the out bound IPs that has been mentioned in the app service.

How can I understand which IP am I using at a time? I'm thinking of storing the token in a dictionary for each IP and then using it.

Is there any better way?

What I've found:

One is like below, but first, I don't like opening a socket separably every time. Secondly, is the IP the same on every call?

using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
    socket.Connect("8.8.8.8", 65530);
    IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
    localIP = endPoint.Address.ToString();
}

Is local IP the outbound or the inbound IP? I've also found this:

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }
    throw new Exception("No network adapters with an IPv4 address in the system!");
}
Ashkan S
  • 10,464
  • 6
  • 51
  • 80
  • Each stamp App Service is hosted on has 4 or more outbound IP addresses, they're listed in the portal -- https://blogs.msdn.microsoft.com/waws/2017/02/01/how-do-i-determine-the-outbound-ip-addresses-of-my-azure-app-service/. Allocation is random, but never outside that list. There's not much to do here, a dictionary won't help since you don't know your outbound IP beforehand. Talk to those guys, they need to be a bit more relaxed with their token logic. This isn't 2005 any more. – evilSnobu Feb 25 '18 at 17:42
  • @evilSnobu do you know if the IP stays the same if I call 2 backend services in the same API call? – Ashkan S Feb 26 '18 at 09:48
  • 1
    Even if that may be true in tests, since there's no guarantee for stickiness, it's a bad start. – evilSnobu Feb 26 '18 at 10:26

1 Answers1

0

How can I understand which IP am I using at a time?

AFAIK, you could not make sure what the IP you current use when generate a token.

This selection is not configurable and the selection algorithm should be considered to be random. You can designate a specific IP address for outbound as described in combined scenarios.

For more details, read this article.

You could modify the rule of your token generator about how to generate token base on your IP address.

And try to change the single IP address to the outbound IP address group. So that, no matter which IP, the token could match with all the outbound IP addresses.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • 1
    The token generator is not under his control, he's calling into it from App Service, which may pick any of the 4 or 5 public outbound IP Addresses when making the connection. – evilSnobu Feb 25 '18 at 17:33