简体   繁体   中英

Connecting to SQL database works when ASP application is hosted in IIS Exress but doesn't work when hosted in Local IIS

In my MVC API web service, I created a simple SQL connection object with a connection string in a Get method. I also created an Ado.net Data Model (with the same connection string) and wrote a LINQ statement to get a row from the database in the same Get method.

When the MVC application is set to IIS Express, both the ado.net connection.open() command and the Entity Framework LINQ statement work and data is retrieved from the database.

However, when I change to Local IIS (I need it to be Local IIS), connecting to the database fails using both ways and this is the error that appears in either way:

错误图像

When you're running with Local IIS using Integrated Security in your SQL Server connection, the connection to your SQL Server instance is being made under the context of the account the IIS App Pool is running as. You need to do one of the following:

  • Change what account your IIS App Pool is using
  • Grant access in the db to the account the IIS App Pool
  • Switch to a SQL Login

This was not the case when using IIS Express because IIS Express runs under the context of YOUR account, which I'm assuming does have permissions to the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM