添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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 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.

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 by 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... ;-)

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! – Justin R. Aug 13, 2009 at 3:22 Just wanted to add that similarly, I resolved this issue by creating permissions for the IUSER username found in IIS 6.0 Web Sites->Properties->Directory Security->Edit (Authentication…) which was IUSR_MACHINENAME – shawad Feb 24, 2015 at 18:27

It's important to note that you'll get this error, as I just did, if you don't have IIS configured to allow impersonation, but you do have your web.config attempting to do impersonation.

I just came across this exact error, and all of the following steps are required:

  • Ensure ASP.NET impersonation is enabled on your IIS web server: enter image description here

  • Combine that with configuring your site to use impersonation (web.config):

    <system.web>
        <identity impersonate="true" userName="your_service_acct" password="***" />
    </system.web>
    
  • The above steps presume that you have a SQL Login setup on your MSSQL for 'your_service_acct' with permissions

    When running on localhost, against a localdb, or even a remote db that you personally have permissions on, the development IIS runs as if it were YOU - and everything just magically works. So, in debug mode, you don't need to create a special web.config..

    As soon as you deploy your site onto some kind of server (in my case, our TEST environment) you'll likely need to have done the above steps I just detailed, because IIS will try to connect as the application pool user, which is not usually what you want administratively speaking. So, that's when you want to start using web.config transformations, so Visual Studio will insert the appropriate identity impersonate="true" during your 'Publish...' deployment step.

    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.

    No! You cannot recommend to use username and password when there's a different use case like authorizing with machine account. – Steve Rakebrandt Nov 15, 2017 at 9:42

    The simple solution is to check your web.config file and make sure one of these is part of the db connection string:

    Trusted Connection=false

    Integrated Security=True

    this worked for me: Open the IIS Manager (inetmgr) In the "Connections" panel, drill down to your site's node and select it In the right-hand panel, under the "IIS" group, double click the "Authentication" icon. Right-click on "Anonymous Authentication" and choose "Edit..." from the context menu. In the popup dialog, select the "Application pool identity" radio button. Click OK.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid …

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •