azure sql server firewall settings

2019-08-06 11:23发布

问题:

How to find my computer's external IP address for azure sql server firewall settings? It is different from the one I get from Ipconfig command(IPv4). I can see a specific IP address on the azure portal but want to know if/how I can see it from my machine?

回答1:

There is a "AutoDetectClientIP" Management API call that updates an existing Firewall Exception to the caller's IP address.

But you need access to a Management Certificate that is valid for the given subscription, the subscription ID, the name of the SQL Azure Server and the name of the Firewall Exception.

Below how you can use that API.

public static bool SetFirewallRuleAutoDetect(string certFilename, string certPassword, string subscriptionId, string serverName, string ruleName)
{
    try
    {
       string url = string.Format("https://management.database.windows.net:8443/{0}/servers/{1}/firewallrules/{2}?op=AutoDetectClientIP",
                                  subscriptionId, 
                                  serverName, 
                                  ruleName);

       HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;

       webRequest.ClientCertificates.Add(new X509Certificate2(certFilename, certPassword));
       webRequest.Method = "POST";
       webRequest.Headers["x-ms-version"] = "1.0";
       webRequest.ContentLength = 0;

       // call the management api
       // there is no information contained in the response, it only needs to work
       using (WebResponse response = webRequest.GetResponse())
       using (Stream stream = webResponse.GetResponseStream())
       using (StreamReader sr = new StreamReader(stream))
       {
           Console.WriteLine(sr.ReadToEnd());
       }

       // the firewall was successfully updated
       return true;
   }
   catch
   {
       // there was an error and the firewall possibly not updated
       return false;
   }
}

Above information comes from here.