I was writing an ASP.Net 2.0 web application using Windows authentication (since I needed to capture users’ credentials). The web application saves data into a MS SQL 2005 database using a SQL server account. While developing this application, I ran into a very strange issue.
Since the database access is using a SQL Server account rather than integrated authentication, in theory the database access code should not be affected by whether I use Forms authentication or Windows authentication in the web application.
However, the problem I ran into was that if I use Forms authentication mode, everything works fine. But if I switch the authentication mode to Windows, I would get the following error message emitted from the data access layer after the application was deployed to the server:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)
What is weirder is that if I tried to use the application locally on the web server after deployment, everything would work fine. And after “touching” the deployed website on the server, the web pages can then be remotely accessed via client browser for a while (10 minutes maybe) but after that the same error would occur again.
So I searched many forums but could not find any conclusive explanations or remedies.
The Not So Satisfying Solution
One of my coworkers took a look at my code and confirmed that there were no coding errors but noticed that I did not have port information in the connection string (Note, even though we use non-standard ports, this has never been necessary). So as he suggested, I plugged in the port number and it worked!
Clearly, I am still as confused as before. The authentication modes differences must have somehow affected the way the connection string is interpreted…