Fixing the annoying IIS error: Cannot open database requested by the login

Cannot open database _Database Name_ requested by the login. The login failed. Login failed for user _User_

While I was developing this blog I got a very nice error right after setup the IIS 7 on my machine:

Cannot open database <Database Name> requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\davidsonsousa.net'.

Why does it happens?

It happens because once you create an Application Pool (in my case davidsonsousa.net), the IIS will create a virtual account with its name (IIS APPPOOL/davidsonsousa.net) and run the Application Pool's worker processes under this account. And, I suppose, as the database doesn't have this account setup you will get this error. [more]

How to fix it?

Before you run to your SQL Server and create this account there I would advice you to do the following:

  1. Open IIS Manager
  2. Go to the Application Pools node
  3. Right click on the Application Pool you would like to change and select "Advanced settings..."
  4. Select the "Identity" list item and click the button with the three dots
  5. In the new window, select the Identity Type "LocalSystem" from the combo box
  6. Confirm everything and restart the website.

Now you will be able to run your website without any problems.