Search code examples
c#sql-serverazureconnectionvpn

Azure Web App is not connecting to On-Site SQL Server via VPN using connection string


I have an Azure Web App running, the site needs to connect to an on-site SQL database. My On-site network with the SQL server has been added via VPN to Azures Virtual Network. When I go to Azure and use the console on my Web App (which I have looked up on how to do on another thread on here) I can tcpping the IP and even run sql commands on it and get results.

However... I take these exact same connection string details that I used in the console and put them in a connection string and the Web App just will not connect.

I am using

<add name="ConSQL" connectionString="Server=tcp:192.168.xxx.xxx:1433;Initial Catalog=LoggerData;User ID=xxx;Password=xxx;Trusted_Connection=False;Encrypt=True;"/>

Is there something I am missing? I know that my Azure Service Web App can see and access the onsite sql server by pinging and querying it from console, but as soon as I launch the web app itself and try and connect to a database as you would in C# it will not connect.


Solution

  • Try going to Configuration>Connection Strings and add the connection string under the web app. Make sure it is name exactly as referenced in your code. Also, in your code make sure you get the connection string from the configuration file.

    services.AddDbContext<YourDBContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("ConSQL"));
            });