title: User permission error when accessing "user instance" database from ASP.NET
body: <p>The server hosting csharpindepth.com has recently been migrated.</p>
  
  <p>Most of it is working fine, but I can't get at the database used to hold errata, notes etc.</p>
  
  <p>Relevant details:</p>
  
  <ul>
  <li>32 bit Windows Server 2003</li>
  <li>SQL Server Express 2005 installed</li>
  <li>ASP.NET pool running under "NETWORK SERVICE" account</li>
  <li>.NET 3.5</li>
  <li>Everyone has full permission to database files (at the moment, just to rule that out!)</li>
  <li><p>Connection string:</p>
  
  <pre><code>Data Source=.;AttachDbFilename=|DataDirectory|\WebSiteData.mdf;
  Integrated Security=True;User Instance=True
  </code></pre></li>
  <li><p>I'm connecting just by creating a new <code>WebSiteDataContext</code> (which has the above connection string as its default)</p></li>
  </ul>
  
  <p>Using a small test console app running from the directory containing the files, as the administrator account, using the same query, I can see the contents of the database.</p>
  
  <p>In ASP.NET I'm getting this exception:</p>
  
  <pre><code>SqlException (0x80131904): User does not have permission to perform this action.
  </code></pre>
  
  <p>EDIT: More information, here's the stack trace:</p>
  
  <pre><code>[SqlException (0x80131904): User does not have permission to perform this action.]
     System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4844759
     System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
     System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
     System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +35
     System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +144
     System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +342
     System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +221
     System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +189
     System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +4859187
     System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +31
     System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +433
     System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
     System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +499
     System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +65
     System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
     System.Data.SqlClient.SqlConnection.Open() +122
     System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user) +44
     System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe() +45
     System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode() +20
     System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +57
     System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator() +35
  </code></pre>
  
  <p>EDIT: I was wrong about the filename having to be right - changing the connection string to make it talk to a different file <em>doesn't</em> change the error.</p>
  
  <p>ProcMon never shows it touching the file...</p>
  
  <p>EDIT: Further oddities - restarting the application pool with it running under the Local System account <em>still</em> has problems, which is about as odd as it gets! It's as if, despite the error message, it's actually trying to do something impossible (e.g. with the wrong path) rather than just being a permissions issue.</p>
  
  <p>EDIT: More information - I've just run my little test console app from a service as "NETWORK SERVICE" and (after an initial attempt which timed out) it was successful. So it's not a permissions thing in terms of the user account... it's something about the environment in which it runs...</p>