ADO.Net - How to Specify Source IP Address in SqlConnection on Multiple IP System

I have a server with a network adapter configured with multiple IPs. Various ASMX and WCF web services are installed on the server and for some security reason for every WEB service I got from my network administrator a dedicated source IP to connect to the specified RDBMS via ADO.Net. With a lot of experimentation, I've come to the conclusion that any SqlConnection object only connects through the first IP configured on the network adapter (the first IP acts as the default source IP). Can I specify the source IP for the SqlConnection object? I have already seen the suggested solution for a similar problem in Specify source IP for ADO connection from Delphi to MySQL , but such solution does not apply in my case.

I need something more similar to the method below, which is used in the case of ASMX web service clients, where I can specify the source IP via a callback:

protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
    System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)base.GetWebRequest(uri);
    webRequest.ServicePoint.BindIPEndPointDelegate += new System.Net.BindIPEndPoint(BindIPEndPointCallback);
    return webRequest;
}
private IPEndPoint BindIPEndPointCallback(ServicePoint sp, IPEndPoint epRemote, int tries)
{
    IPAddress ipLocal;
    EndPoint epLocal;
    IPAddress.TryParse(strIPAddr, out ipLocal);
    if (tries <= 3)
    {
        epLocal = new IPEndPoint(ipLocal, 0);
    }
    else
    {
        throw new Exception(string.Format("BindIPEndPointCallback: error connecting via {0}", strIPAddr));
    }
    return (IPEndPoint)epLocal;
}

      

Is there something similar for the SqlConnection object?

+3


source to share


1 answer


I am not aware of any settings on the SQLConnection object to specify a local IP address.



However, you can configure your own Windows Firewall rule to block remote IP (SQL) for source IP addresses (alternate network adapters).

0


source







All Articles