views:

2619

answers:

2

My ASP.NET v3.5 web application is throwing the following exception when it attempts to open a connection to a SQL Server 2008 database:

System.Data.SqlClient.SqlException: Cannot open database "MyDbName" requested by the login. The login failed. Login failed for user 'NT AUTHORITY\IUSR'.

The thing is, I've added NT AUTHORITY\IUSR to the the server's list of logins, and to the database's list of users. For the server, I've granted the user the Public role, and for the database I've granted db_datareader permissions.

I've also granted the same for NT AUTHORITY\NETWORK SERVICE, which is the identity that the application pool is running under.

The web application is hosted by IIS7, if that makes a difference. The problem repros when the DB and IIS are on the same physical machine as well.

+1  A: 

The trick here is that NT AUTHORITY\NETWORK SERVICE actually appears to the database as DOMAINNAME\MACHINENAME$ (note the $ sign!). That is, when you cross the machine boundary from your web server to the SQL Server, SQL Server sees the machine account if you use the NETWORK SERVICE or LOCAL SYSTEM accounts. If you use any other non-domain account, SQL Server will not receive your credentials. I'm a bit puzzled at your error message. Truth be told, I don't think that when the DB is on another box, you'll see anything other than "Login Failed for NT AUTHORITY\ANONYMOUS LOGON".

IUSR is used for anonymous websites, and can't pass over the wire to SQL Server. You may find a way for it to work if you're doing everything on the same machine, but I'd never know because I'd never do it that way... ;-)

Dave Markle
I was just testing to see if it'll all work on the same box - in production the topology is best described as sprawling. Anyway, you nailed it - I had to add permissions for the machineName$. Thank you!
fatcat1111
A: 

I would suggest to create a separate (preferably domain) account and specify it in the connection string (usually in web.config) Then you can limit permissions on the web server what this account can and cannot do. Then you can grant this account required permissions in SQL server.

DmitryK