How can I connect to my SQL server from my VM using a .NET application.

Nicholas Pallotta 0 Reputation points
2025-02-07T23:50:29.9033333+00:00

I have a .NET application on my azure virtual machine that will not connect to my azure SQL database. I can otherwise connect to the db on my vm using SSMS. My .NET application will connect from my local laptop. I am using ENTRA ID authentication.

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Other
SQL Server | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Raymond Huynh (WICLOUD CORPORATION) 620 Reputation points Microsoft External Staff
    2025-07-30T10:01:42.1533333+00:00

    Hello,

    Based on the thread, here are some additional solutions that haven't been mentioned yet:

    Network and Environment Checks:

    • Verify your VM's outbound internet connectivity isn't blocked by NSG rules
    • Check if there are any proxy settings or corporate firewalls on the VM that might interfere with Azure SQL connections
    • Ensure the VM can reach *.database.windows.net on port 1433

    Authentication Context Issues:

    Since you can connect via SSMS but not your console app, this suggests the VM has different user context than your local machines. Try these approaches:

    1. Run the console app as the same user account that successfully uses SSMS
    2. Test different authentication methods in your connection string: In your .NET console application, modify your connection string to try these authentication methods one at a time:
       Authentication="Active Directory Interactive";
       Authentication="Active Directory Password"; 
       Authentication="Active Directory Service Principal";
    

    Replace the Authentication="Active Directory Default"; part in your current connection string with each of these options and test.

    VM-Specific Troubleshooting:

    • Install the latest Microsoft.Data.SqlClient NuGet package on the VM
    • Ensure the VM has the proper .NET 8 runtime and all dependencies
    • Check if there are any antivirus or security software blocking the connection
    • Verify the VM's system time is synchronized (authentication tokens are time-sensitive)

    Debugging Steps:

    • Add detailed logging to your console app to see exactly where the connection fails
    • Test with a simple connection string first, then gradually add complexity
    • Use SqlConnection.ConnectionTimeout to determine if it's a timeout vs authentication issue

    Alternative Authentication:

    • Consider using Managed Identity if your VM supports it
    • Try certificate-based authentication
    • Use a service principal with client secret authentication

    The key insight is that "Active Directory Default" authentication relies on the current user context, which differs between your local machines and the VM environment.

    Hope this helps solve your connection issue! Let me know if you need any clarification on these approaches.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.