ASP.NET Core - Application not connecting to database after publishing - asp.net

I created a simple ASP.Net Core application with user authentication (so all the Entity Framework has been preloaded into the web app template). It is connecting to my database with the connectionString that is located in my appsettings.json file, with "data source = {computerName}\\{serverName}" setup.
The database instance and Visual Studio are located on the same machine. This works fine in returning data to the web api while within development and debugging mode. When I publish it and try to go to the site from a domain name it does allow me to view the webpages that are not needing database connection but the rest that need data from the database returns pages like this:
Error.
An error occurred while processing your request.
Development Mode
Swapping to Development environment will display more detailed information about the error that occurred.
Development environment should not be enabled in deployed
applications, as it can result in sensitive information from
exceptions being displayed to end users. For local debugging,
development environment can be enabled by setting the
ASPNETCORE_ENVIRONMENT environment variable to Development, and
restarting the application.
I have tried to change the Environment Variables for ASPNETCORE_ENVIRONMENT from Development to Production to no success.
I have tried adding appsettings.Production.json to publishOptions in my project.json file, even though there is no file appsettings.Production.json, and this did not help. dotnet publish
Adding the evironment variable in the web.config file did not work Deployment
I need help getting the published web api to connect to my SQL Server database from outside the development Visual Studio setup.
The last thing I can think of is that maybe I am incorrect in how I understand the connection string. If the web api uses the connection string to connect to the database from the server-side then it should work just fine like it does when in development calling to http://localhost:port# since it's all on the same machine. But, if the database string needs to be based on a client-side call then it would have to be with domain names and IP addresses.
Can someone tell me which one it is?
The only thing else that comes to mind is that there is something I am not doing, and need to do, inside of IIS Manager. I see connect string there as well but unsure what that is for our does since the connection string is inside the application. Also maybe I am suppose to give the app some kind of authorization to communicate with the database server even though they are on the same machine???

After much research, finally googling "how to deploy a web api in iis" I was able to learn from Host ASP.NET Web API in IIS using Visual Studio Publish that I needed to add the security entity BUILTIN\IIS_IUSRS. Then placing a mapping to my database tables and giving db_datareader (and possibly db_datawriter) for the database to the IUSER to allow access from my self hosted IIS web api. This from the above mentioned link with the part stating
Accessing Database under IIS APPPOOL\ASP.NET v4.0
As we are using ASP.NET v4.0 App Pool make sure IIS APPPOOL\ASP.NET v4.0 is added to your Database Server -> Security -> Logins.

Related

Unable to connect to database with .NET Core 3.1 File System Publish to IIS

Question: How do I properly setup the database connection when using the File System method to publish an ASP .NET Core 3.1 Web-Api to IIS running on Windows 10? I believe I have added all of the correct Windows features for IIS because it runs and I am running VS 2019 Community in Admin mode.
This is my first time deploying a .NET Core 3.1 Web-API to IIS via the Publish method. I am able to successfully publish the web application to IIS. But it appears that something is wrong with the database configuration because when I try submit a login POST via Postman, I am getting an error message from the API error handling middleware that the credentials are invalid, which effectively means that the Web-API cannot connect to the database. As a test, when I try running in debug with IIS Express and change the connection string, I get the same error. Note: the IIS server logs do not contain any relevant output to confirm this, but I am pretty sure this is the problem.
In searching online, I am struggling to find a simple and direct walk through on how to publish a Web-API with a database connection. I suspect the problem is in the Connected Services section of the publish profile. I had to battle through some build errors that related to missing packages, and this item:
https://obscureproblemsandgotchas.com/development/c/dotnet-core-ef-migration-not-working/
Once I applied this change, the database data context strings could be read from the Publish Settings section of the publish profile, which reads the connection strings from the appsettings.json file. Though, I had to modify that string to remove an escape '' from the actual json value so that the test connection would succeed.
Any help is greatly appreciated.
You could set the connection string at the time of deploy from the visual studio as shown below :
Make sure you installed the .NET Core Hosting Bundle and .NET Core Runtime.
after doing all these things set the application pool identity to the administrator:
Open iis manager, select your application pool.
Click on advance setting from the action pane.
Under Process Model, click on the “Identity” value and select “Custom account”.
enter your domain user name and password which user has to access to the database and click ok to apply the changes.
after applying changes select the application pool and click on the “Recycle”.
restart your site.
Please refer below link for more detail:
https://forums.iis.net/post/2159167.aspx

How to access SQLite DB from IIS hosted WCF Service?

I am trying to deploy a WCF service that uses a SQLite DB to read some data as well as connecting to another Database (MS SQL). The service itself is consumed by a ASP Web site running on the same server.
The problem is that only my user is able to fully use the website and service. Every other user can't seem to open the sqlite database file.
What kind of permissions or special access do you need to access a sqlite database from IIS?
The Application pool is setup with a domain account so that access to the MS SQL Database can be granted (Integrated Security).
The folder where the database is stored is accessible by the account. And I've even tried to make it accessible by Everyone and all users with no luck.
I feel I'm missing something obvious but I can't figure out what...
Update: The error is as follows:
System.Data.Entity.Core.EntityException
An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.dll but was not handled in user code
Additional information: The underlying provider failed on Open.
Inner Exception:
System.Data.SQLite.SQLiteException
Unable to open database file.
I was able to resolve the issue, I'm going to document it here for posterity. I took a few steps to do so, and I currently don't have the environment set up to be able to reproduce the issue consistently and quickly, so I'm not entirely sure which was the actual solution.
Disclaimer aside, here are my changes:
I created a new Application Pool and Website on IIS, on another directory entirely, not on "C:\inetpub\wwwroot". I used "C:\Sites\MySite" to deploy both my WCF service and the website which consumed it.
I changed the application pool identity account to a new AD Service account.
I placed the database inside of the Service Web App directory, within a directory called data. I removed all inherited permissions from the directory and the database file, and gave the both the service account and Everyone Full Control of the directory and database file.
I edited the connection string for sqlite to have the setting "Journal Mode=Off;".
I stopped and started both the Application Pool and the WebSite on IIS.
After this the service worked correctly, and I was able to access both the database file and the connection to SQL Server. If I get the opportunity, I would set up some kind of virtual network to replicate this and see if I could figure out exactly what caused the error in the first place.

Can't Access SQLite DB on Azure Web App

I have created an ASP.NET MVC web app which uses an SQLite database under EF6.
The app works fine in VS. The database is included in the project in the App_Data folder. When I publish to Azure (or an IIS test server on the LAN) I get an error. The Azure instance is showing an HTTP 500 error, and it only happens on views which require database access to load. Other than that it works fine. The IIS test server shows an error saying unable to open database file. I tried loosening file permissions on the .db file on the IIS instance, but nothing changed. I'm not sure how to go about doing anything on the Azure instance.
Can someone please assist me with debugging a SQLite connection problem on Azure/IIS?
Details
VS 2015 Enterprise
Azure app services (free tier, production server)
SQLite3 database
IIS on Windows Server 2012R2 (test server)
I have tried all sorts of combinations of connection strings:
setting a connection string to the D:/xxx/xxx path in Azure Portal
modifying the connection string in the deployment profile in VS (replaces value in web.config upon deployment)
just leaving it all the same
Nothing has worked or even changed the error. Perhaps one of those configurations is able to access the file but simply can't open it (which is sort of what the IIS error suggests, I think). Even then, how do I go about finding out?
EDIT
On the IIS test server I discovered that the entire problem was that the connection string in Web.config was wrong. I manually edited it on the server and it fired right up. I'm still not sure how to correct the path on the Azure server, nor how to correct my deployment settings in VS to inject the correct path. Thoughts along those lines are still appreciated.
EDIT 2
I've also found that each time I hit a page requiring DB access, I see this error show up in the application logs on Azure:
2017-01-03T20:25:32
System.ApplicationException: The trace listener AzureBlobTraceListener is disabled. ---> System.InvalidOperationException: The SAS URL for the cloud storage account is not specified. Use the environment variable 'DIAGNOSTICS_AZUREBLOBCONTAINERSASURL' to define it.
at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureBlobTraceListener.RefreshConfig()
--- End of inner exception stack trace ---
After some abysmally slow debugging sessions, I finally remembered that in toying around I had set a connection string value in the Azure portal.
Apparently whatever value is set in the Azure Portal overrides (at runtime?) the values in the Web.config file
I simply deleted the connection string from the Azure Portal (your app/Application Settings/Connection Strings) and it worked. The final connection string looks like this:
<add name="yourThingEntities" connectionString="metadata=res://*/cadb_SQLite.csdl|res://*/cadb_SQLite.ssdl|res://*/cadb_SQLite.msl;provider=System.Data.SQLite.EF6;provider connection string="data source=D:\home\site\wwwroot\App_Data\yourSQLiteDBFile.db"" providerName="System.Data.EntityClient" />
To get the Web Deploy working in VS, you'll need to edit the publish profile to have a connection string matching the format
data source=D:\home\site\wwwroot\App_Data\yourSQLiteDBFile.db
Apparently not many people use SQLite for web apps, and most people should not. If you ever expect more than one user for your website at a time, you shouldn't fix SQLite, you should convert to SQL Server CE or just full-blown SQL Server which Azure will take care of for a paltry $5/mo. Choose wisely!

ASP.NET MVC Entity Framework application is using wrong SQL connection string

I have an ASP.NET MVC5 web application that was originally created using VS2013 and uses Entity Framework 6 Code First, with data migrations. It has been working fine (in production) for almost a year and has been re-deployed numerous times, from Visual Studio, using Web Deploy.
Today, I opened the solution in VS2015 and redeployed a known working version of the codebase to production without any problems. However, after deployment, the production website suffered from the problems described in this article, which I solved by applying author's the recommended fixes.
After applying these fixes, I now have a new problem: an exception that suggests that the application is trying to access the database using the wrong connection string:
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.
Within the details of the exception page, the cause of the problem is clarified:
The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory.
I'm puzzled by this because the application has a dedicated SQL Server instance that is accessed using a regular connection string. I've checked the Web.config generated by Web Deploy and it seems to be intact. It contains a named connection string that correctly references the production SQL Server instance, and the code (which hasn't changed) correctly instantiates an Entity Framework ApplicationContext using the named connection string.
Does anyone have any idea why this application now expects to create its own SQL Server instance or what I can do to debug this problem?
Many thanks for your suggestions,
Tim
A few things you can try before making the web deploy:
Check if the project that contains the connection string declaration (web.config) is setted as the 'startup project'.
Rebuild the solution in release mode.
In the Publish Web wizard, in Settings, expand File Publishing Options and check 'Remove additional files at destination' and 'Exclude files from App_Data folder'

Publishing ASP.NET site - No login database?

I have a very simple app on my local machine that uses the ASP.NET membership provider. This works fine on my local machine.
I have published the app to our web server, and I need to know the specifics of setting up the application for asp.net membership to work.
I have a basic understanding that it uses an MDF file and somehow connects to a SQL database (SQLEXPRESS I assume on my local machine) which holds the user/role information.
I keep getting an error that it cannot connect to a SQL database when running the published application on the web server.
I wish to use a SQL server on a different machine (SQL69) than my web server (WEB69)
I have created a new connection string, but how do I tell ASP.NET to use it instead of the "LocalSqlServer" connection string that is created by default?
Can someone explain what is required to me?
Thanks for any help you can offer.
Use the ASP.NET SQL Server Registration Tool without any parameters to set up the SQL Server membership on your local machine. This will also update your config file:
http://msdn.microsoft.com/en-us/library/ms229862%28VS.80%29.aspx
You need to set up SQL Server DB membership on your local machine first. After you have this working, you can simply script the entire DB for your production environment. For this, you can use the Database Publishing Wizard in VS.
You can view a sample config file here:
http://www.codersbarn.com/post/2008/02/24/ASPNET-20-Guest-Book-Admin-Part-II.aspx

Resources