Running into an SQL Error with an MVC Application deployed on IIS - sql

So I am new to working on web projects in general. I am working on an MVC application in Visual Studio 2008. I have generated an SQL database within VS, and I have deployed my application on IIS. However when I try to do anything in the application which will spark an SQL query, I get the following error:
"Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path. Please make sure the user has a local user profile on the computer. The connection will be closed."
I have hit google on this problem, and people have suggested a number of solutions but I am unsure how many of them are relevant to me doing this when I'm doing this in VS2008,. I have tried a few simple things suggested like setting the trust level to full, and setting the Load User Profile to true in IIS, but no luck yet.

What edition of SQL are you running where you deployed the application to? If it is not SQL Express, you will need to remove "User Instance=True" from the connection string.

Related

How to make IIS use AppPool Identity to connect to SQL Server instead of NT Authority\IUSR?

I've set up a Windows Server 2019 box with IIS, PHP 8 and SQL Drivers, and SQL Server 2019 Express. I created a test PHP application and corresponding application pool, then set up a test database. When trying to connect using sqlsrv_connect, I get the following error:
Login failed for user 'NT AUTHORITY\IUSR'.
(There is more there in the SQL error but it's just the raw error array output and Stack Overflow doesn't like it.)
Now, I believe I have everything configured correctly to use the application pool identity instead of NT AUTHORITY\IUSR. I copied the setup from another older (Server 2012) web server. Screenshots of my setup:
Anonymous Authentication using App pool identity in IIS
App pool identity added on database in SQL Server
Am I missing a setting somewhere, or does Server 2019 handle application pool identities differently than server 2012? I've tried following instructions like on this excellent SO page:
IIS AppPool to SQL Server permissions (adding NT AUTHORITY\IUSR)
But it's like the server doesn't care that I've told it to use the application pool.
UPDATE EDIT: MYSTERY SOLVED
Apologies all, this was a case of extreme user error. I put the "test" directory under wwwroot, ala c:\inetpub\wwwroot\test. I was accessing it as www.domain.com/test, when what I needed to do was set it to run as test.domain.com. It was running under the wwwroot app pool (which in this case was using IUSR) because that is how I was accessing it. Thank you everyone who helped me troubleshoot this!
Apologies all; this was a case of extreme user error.
I put the test directory under wwwroot, i.e., c:\inetpub\wwwroot\test. I was accessing it as www.domain.com/test, when what I needed to do was set it to run as test.domain.com. It was running under the wwwroot application pool (which, in this case, was using IUSR) because that is how I was accessing it.
Thank you everyone who helped me troubleshoot this!

How to allow others to run Web App using LocalDb

I have created this assignment where I am essentially using .net Core to read and write to an instance of a SQL localDb: Github
This is my first time using a localdb, but when I submitted it to my professor, he is not able to create, or manipulate the database at all. My thoughts were that when he runs it, it would create and instance of the database on his computer, but I guess not. He gets the following error:
SqlException: Cannot open database 'Bartender_App' requested by login. The Login failed. Login failed for user "Username"
this error occurs when trying to create a new order. Is there any way to bypass this authentication? because the web app works on my computer.
would he have to create his own migrations and update the database from Visual Studios on his end?
I have tried to look it up, but come across deployment of these web apps to a server which is not what I am trying to do, I am just aiming for him to be able to run the application on his computer, even if the database starts empty, and he can manipulate, add, and remove the data from the localDb instance
I apologize in advance for the tags - the autocomplete feature was not showing up correctly.
Is there any way to bypass this authentication? because the web app
works on my computer.
This is impossible, SQL Server Express LocalDB does not accept remote connections.
As an alternative, you can automate the backup of your database structure and provide it to your instructor, you can refer to How to copy a database from one computer to another?.
If there is not much table, manual creation may be faster and easier.
More details ,have a look for Can SQL Server Express LocalDB be connected to remotely?.

Lightswitch 2012 SQL server login error

Environment
Windows 7 machine running VS2012 Update 2
Server 2008 box running SQL Express 2012
Happily modifying existing Lightswitch application that connects to the SQL Server Express system using SQL Authentication. All of a sudden I start to get all sorts of funny errors (something weird happened at this point) so I stopped what I'm doing and restart both server and Win7 system.
When I now try to run/build my LS application it gets an error stating that login to the SQL Server failed because login is from an untrusted domain and windows authentication failed.
Problem is I am using SQL authentication??? I have triple checked my web.config settings and the connection string is definitely set for SQL auth. I created another "test" application, again using SQL auth and it works fine. (I compared the config files from app to app and the connection strings are identical.
In the VS IDE I have Data Connections defined to the SQL server using SQL auth and it works fine. If I do an "update" of the data source and check connection dialog it uses SQL auth and the update works fine.
But when I try to build/run the app it wants to use Windows Authentication.
(I am not using any application/screen level security in my app).
Got me stumped.
Any ideas?

IIS connecting to LocalDB

Is there any way so IIS could connect to LocalDB without using the NT SERVICE\NETWORK SERVICE user account.
This account has not suitable permissions. I'm looking use some other default account or is there some way that I can use the NETWORK SERVICE account without changing permissions?
You should use Shared Instances feature of LocalDB. These two posts on Using LocalDB with Full IIS should give you more information. Especially the second part seems relevant, but the first one contains some context as well.
(note: the original links are no longer available, using archive.org instead)
Part 1: User Profile
Part 2: Instance Ownership
Original (non-working as of March 2019) links:
Part 1: User Profile
Part 2: Instance Ownership
In case the links disappear again, I am copy-pasting solutions from the article for easier access:
Post 1:
The problem we're facing is that the user profile needs to be loaded. That shouldn't be hard since each IIS Application Pool has an option called Load User Profile that can be found in Advanced Settings section. Unfortunately things got slightly more complicated in Service Pack 1 for Windows 7. As described in KB 2547655 enabling loadUserProfile is not enough to fully load user profile, we also need to enable setProfileEnvironment. This requires editing applicationHost.config file which is usually located in C:\Windows\System32\inetsrv\config. Following the instructions from KB 2547655 we should enable both flags for Application Pool ASP.NET v4.0, like this:
<add name="ASP.NET v4.0" autoStart="true" managedRuntimeVersion="v4.0" managedPipelineMode="Integrated">
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</add>
Having completed that we restart the Application Pool to make sure the new settings are applied and run our Web Application again.
Note from my side: Just find "applicationPools" tag in that applicationHost file and update those two variables to true, so it looks like this:
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
That's it, save the file and restart IIS pool.
Post 2:
The Problem of the Private Instance
As we can see we are facing the following error:
System.Data.SqlClient.SqlException: Cannot open database "OldFashionedDB" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\ASP.NET v4.0'.
This time the error is quite clear. LocalDB was started and the Web Application was able to connect to it, but the connection was then terminated due to login failure. The ApplicationPoolIdentity account for the IIS application pool (in this case IIS APPPOOL\ASP.NET v4.0) couldn't login to LocalDB instance because the database specified in the connection string (OldFashionedDB) wasn't found. How odd, since connecting from Visual Studio with the same connection string succeeds!
How is it possible that Visual Studio connects to LocalDB just fine, while the connection from Web Application fails? In both cases the connection string is the following:
Data Source=(localdb)\v11.0;Initial Catalog=OldFashionedDB;Integrated Security=True
The answer is that there are two different LocalDB instances here. Unlike SQL Server Express instances, which are running as Windows services, LocalDB instances are running as user processes. When different Windows users are connecting to LocalDB, they will end up with different LocalDB processes started for each of them. When we connect to (localdb)\v11.0 from Visual Studio, a LocalDB instance is started for us and runs as our Windows account. But when Web Application, running in IIS as ApplicationPoolIdentity, is connecting to LocalDB, another LocalDB instance is started for it and is running as ApplicationPoolIdentity! In effect, even though both Visual Studio and Web Application are using the same LocalDB connection string, they are connecting to different LocalDB instances. Obviously the database created from Visual Studio on our LocalDB instance will not be available in Web Application's LocalDB instance.
A good analogy to this is My Documents folder in Windows. Say we open Visual Studio and create a file in our My Documents folder. Then we login to the same machine as a different user and go to My Documents folder again. We won't find the file there as My Documents of the second user and our My Documents are two different folders. Similarly LocalDB instances (localdb)\v11.0 owned by two different users are two different processes with two different sets of databases.
This is also the reason the Web Application was able to connect to LocalDB from IIS Express. Just like LocalDB, IIS Express is a user process. It is started by Visual Studio and runs as the same Windows account as the Visual Studio process. Two different processes running as the same Windows account (Visual Studio and IIS Express, both running as our Windows account) connecting to (localdb)\v11.0 are connecting to the same LocalDB process, also started as the same Windows account.
Possible Solutions
Understanding the nature of the problem brings multiple approaches to solving it. As different approaches have different tradeoffs, instead of prescribing one solution, below I presented three approaches that seem most viable to me. My hope is to hear from you about the one that worked best for you! Here is the list:
Approach 1: Run IIS as our Windows user
Approach 2: Use LocalDB Shared Instance
Approach 3: Use full SQL Server Express
Let's take a closer look at each of them.
Approach 1: Run IIS as our Windows user
If different user accounts are the problem, why not try to run our Web Application under our Windows account? Web Application would connect to the same LocalDB as Visual Studio and everything should just work.
Making the configuration change is relatively easy, just start IIS Manager and find the right Application Pool:
Open Advanced Settings screen (available in the context menu):
Click the little button in the Identity property to bring up the Application Pool Identity screen:
Starting the Web Application again will confirm that the problem is solved:
What are the drawbacks of this approach? Of course running Web Application under our account brings certain security risks. If someone hijacks our Web Application they will be able to access all system resources our account can. Running the Web Application as ApplicationPoolIdentity provides additional protection since ApplicationPoolIdentity accounts have very limited access to local system resources. Therefore I cannot recommend this approach in general, but when used with care it is a viable option in some cases.
Approach 2: Use LocalDB Shared Instance
We could also use an instance sharing feature of LocalDB. It allows us to share a LocalDB instance with other users on the same machine. The shared instance will be accessible under a public name.
The easiest way of sharing an instance is to use SqlLocalDB.exe utility. Just start an administrative command line prompt, and type the following command:
sqllocaldb share v11.0 IIS_DB
It will share the private LocalDB instance v11.0 under the public name IIS_DB. All users on the machine will be able to connect to this instance, using (localdb).\IIS_DB as a server address. Note the . before the instance name, indicating this is a shared instance name. We should replace the connection string in our Web Application with an updated one:
Data Source=(localdb)\.\IIS_DB;Initial Catalog=OldFashionedDB;Integrated Security=True
Before the shared instance can be used by the Web Application we need to start it and create logins for the ApplicationPoolIdentity. Starting the instance is easy, simply connecting to it from SQL Server Object Explorer will start it and keep it alive. Once we are in the SQL Server Object Explorer we can also create the login for ApplicationPoolIdentity. We could use the following query:
create login [IIS APPPOOL\ASP.NET v4.0] from windows;
exec sp_addsrvrolemember N'IIS APPPOOL\ASP.NET v4.0', sysadmin
This script gives full administrative access to our LocalDB instance to the ApplicationPoolIdentity account. Whenever possible, I would recommend using more limited, database-level or even table-level permissions.
Now we can run our Web Application again. This time it should work just fine:
What are the drawbacks of this approach? The main one is that, before Web Application can connect to the shared instance, we need to make sure the instance is started. For that to happen the Windows account that owns the instance must connect to it and the connection must be kept open, or the LocalDB instance will shut down.
Approach 3: Use full SQL Server Express
Since full IIS runs as a service, maybe using traditional, service-based SQL Server Express is the right approach? We could just install SQL Server 2012 Express RC0 and create the OldFashionedDB database in it. We can even use our brand new SQL Server Data Tools to do it, as it works with any SQL Server version and edition. Our connection string would have to change to:
Data Source=.\SQLEXPRESS;Initial Catalog=OldFashionedDB;Integrated Security=True
Of course, just as in the previous case, we would need to make sure the ApplicationPoolIdentity account has access to our SQL Server Express instance. We can use the same script as previously:
create login [IIS APPPOOL\ASP.NET v4.0] from windows;
exec sp_addsrvrolemember N'IIS APPPOOL\ASP.NET v4.0', sysadmin
After that, running our Web Application brings the happy picture again:
What are the drawbacks of this approach? Obviously we lose the benefits of using LocalDB. Installing SQL Server Express may take more time than LocalDB, and there may be some machine cleanup necessary for it to succeed. SQL Server Express Setup can be blocked by problems like corrupt WMI database, polluted registry or components left by SQL Server or Visual Studio CTPs and Betas. And SQL Server Express will continue running in the background even when not needed, as services do.
Other options
There are other approaches of using LocalDB under full IIS that are not covered here. We could embrace the Web Application's private LocalDB instance and communicate with it through the Web Application by executing T-SQL scripts from ASP.NET code. We could also use AttachDbFileName option of ADO.NET connection strings and use a database file (.mdf) that would be attached to both our LocalDB during development and Web Application's LocalDB for debugging. I tried both I found them too cumbersome to discuss further.
Based on the answer from #KrzysztofKozielczyk.
I originally posted an answer here:
https://stackoverflow.com/a/62810876/3850405
After following this I verified that Load User Profile was set to true for my Application Pool and then set setProfileEnvironment to true in applicationHost.config. I did the last part by editing applicationHost.config located at:
C:\Windows\System32\inetsrv\config\applicationHost.config

Report Server not available after Team Foundation Server 2008 SP1 install

We installed SP1 on our Team Foundation Server 2008 server. Everything seems fine after the install, except there is a red X on the Reports folder in the Team Explorer in Visual Studio. If we attempt to access the Reporting Services web site, we get a message that says that the "report server is unavailable". There were no errors during the installation.
The Reporting Services service started up fine and left no errors in the event log. We looked at the Reporting Services Configuration Tool and everything shows as OK. We tried restarting the RS service and rebooting the machine. Again, no errors but still no report availability.
The SQL Server instance where our data lives is up and running fine and we can query the OLAP cube and the relational side with no problem. All of our developer machines are already at VSTS 2008 SP1. Visual Studio is not installed on the server.
We looked at the logs files in the RS folder and the only errors that show are the ones that we get when we try to access the web site from IE, but that don't seem to contain any more info about the root cause.
Any help would be greatly appreciated. I'll post any updates on other things we try here.
UPDATE: There is a login error showing up the ReportServer(timestamp).log file. The login on the reporting services service is not the login that is showing up in this error, so I'm not yet sure what process is actually trying to make the connection to the db.
We got this resolved. It appears that the TFS 2008 SP1 install process decided to change the identity setting of application pool for the Reporting Services web site. To resolve we needed to:
Open Internet Information Server Manager
Go into Application Pools
Right click on the Reporting Services application pool
Click on the Identity tab
Change the account from NetworkService to our TFS domain account
Edit the rsreportserver.config located in "\Program Files\Microsoft SQL Server\MSSQL.1\Reporting
Change the "NT Authority\NetworkService" to "OurDomain\TFSDomainAccount"
Restart IIS
Hope this saves someone else some time.
Check the IIS application pools. My feeling is that reporting services doesn't play nice with other web applications (usually I've fixed this in the past by setting a seperate pool for the other applications).