title: Access Denied to SQL Server in Microsoft Azure Web Application
body: <p>I have a web application that is published to Microsoft Azure. There are two databases, both hosted on a SQL Server in Azure. One database, which handles identity authentication, works just fine and I can login/view pages that don't concern the other database just fine. When I try to perform an action that accesses the other database, I get the error at the bottom of this post.</p>
  
  <p>It has to be something in Azure. When I run the application locally it works fine, even though it is still accessing the same Azure-hosted databases. Here are the connection strings (I've changed some sensitive info) I'm using which, again, both work fine locally:</p>
  
  <pre><code>&lt;connectionStrings&gt;
  &lt;add name="IdentityConnection" connectionString="Server=tcp:{server}.database.windows.net,1433;Database=identity; ID={User};Password={pw};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
    providerName="System.Data.SqlClient" /&gt;
  &lt;add name="DataConnection" connectionString="Server=tcp:{server}.database.windows.net,1433;Database=Data;User ID={User};Password={pw};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Access is denied</p>
  
  <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
  
  <p>Exception Details: System.ComponentModel.Win32Exception: Access is denied</p>
  
  <p>Source Error: </p>
  
  <p>An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</p>
  
  <p>Stack Trace: </p>
  
  <p>[Win32Exception (0x80004005): Access is denied]</p>
  
  <p>[SqlException (0x80131904): 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)]
     System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action<code>1 wrapCloseInAction) +5341687
     System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +546
     System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5353471
     System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
     System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +923
     System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +311
     System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +646
     System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +278
     System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
     System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +732
     System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +85
     System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection) +1057
     System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection) +78
     System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection) +196
     System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions) +146
     System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions) +16
     System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource</code>1 retry) +94
     System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110
     System.Data.SqlClient.SqlConnection.Open() +96
     License_Project_Web.View_Licenses.Page_Load(Object sender, EventArgs e) +43
     System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
     System.Web.UI.Control.OnLoad(EventArgs e) +92
     System.Web.UI.Control.LoadRecursive() +54
     System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772</p>