Receiving an error doing code first MVC web deployment - asp.net-mvc

I have set up a remote server running SQl Server Web edition and full IIS 7.5. I am able to deploy my MVC 4 application to the remote box without issue, but when I try to run the website I receive the following error:
Login failed for user 'IIS APPPOOL\backoffice.mysite.com'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'IIS APPPOOL\backoffice.mysite.com'.
The Error Console says:
Exception information:
Exception type: InvalidOperationException
Exception message: The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
at MyModels.Filters.InitializeSimpleMembershipAttribute.SimpleMembershipInitializer..ctor() in c:\Users\Administrator\Documents\MySite\MyModels\Filters\InitializeSimpleMembershipAttribute.cs:line 47
An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.
It is very possible that my connection string is incorrect as the error log suggests. My connection running locally in Visual Studio is:
<add name="MyDB" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDB.mdf" providerName="System.Data.SqlClient" />
I have set up a transformation for the connection on the remote server as:
<add name="MyDB" connectionString="Data Source=(local);Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient" />
Normally I use SQL login so I am confused here. What should the connection string look for SQL Server Web Edition using Windows Authentication?
It looks like the IIS APPPOOL\backoffice.mysite.com is the trying to create the DB. Is this correct? If so do I not need to create a login and user for this user in SQL Management Studio?
Do I need to create the DB in SQL Management Studio before running the deployment?
Thank so much!

you are running using Windows Authentintication for SQL Server. The IIS App is running under the user context of 'IIS APPPOOL\backoffice.mysite.com'; I would strongly suggest using either standard sql server credentials or creating a new user for that particular website that has sql credentials, probably better with regular credentials though. Integrated Security=SSPI in the connection string means Windows Authentication. So just use standard SQL Server credentials and set your database to mixed mode auth and set the User ID and Password in your connection string and then you will be up and running no problem.

Related

Not able to get Azure Key-Vault working in app when deployed to Azure

I have created an app using MVC.net 4.7.2 application to use Key Vault secret for connection string. For some reason it is not working when deploying to the Azure. Below are the steps. Please suggest the solution based on .net not .net core.
I have created a MVC.net 4.7.2 application. Implemented Azure AD Authentication and tested my app, it is working fine no issue.
Then I created a Key Vault secret for the database connection string. And give permission to my app from the Key Vault->Access Policy
On the application level. Used the Connected Service to access the Key Vault-> Secret for connection string. That added the code in webconfig and added nuget packages. See the code below.
Run the code locally which has no connection string and I was able to get the data. It worked perfectly without any issue. I was also able to determined that app is picking up Key Vault secret for connection string.
Deployed the app to the Azure app service.
Ran the application but I did not get the data however see the error as if the connection string is not provided.
Error
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found
or was not accessible. Verify that the instance name is correct
and that SQL Server is configured to allow remote connections.
(provider: TCP Provider,
error
What is it that I suppose to do or check in order for it to work. Note that locally, I am able to get the data with the code that I have in my webconfig. So seem like there is no issue. And also I have Azure AD identification working.
Web Config code:
<configSections><section name="configBuilders"
type="System.Configuration.ConfigurationBuildersSection,
System.Configuration, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f22d50a3a"
restartOnExternalChanges="false"
requirePermission="false" />
<builders>
<add name="AzureKeyVault" vaultName="Prod-ConSt-01"type" Microsoft
.Configuration.ConfigurationBuilders.AzureKeyVaultConfigBuilder,
Microsoft.Configuration.ConfigurationBuilders.Azure,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31bf9256ad364e35"
vaultUri="https://mywebsite-prod-const-01.vault.azure.net/" /><
/builders>
</configBuilders>
<connectionStrings configBuilders="AzureKeyVault">
<add name="ProductionConnstr" connectionString="from key vault"
providerName="System.Data.SqlClient" />
</connectionStrings>
This isn't a Key Vault problem - it seems that it's accessing the Key Vault just fine since it is trying to contact the database. Check your allowed IP range to allow your website to contact the database.

Login failed for user 'sa' ASP.NET MVC page

I'm trying very hard to go from Entity Framework 5 to 6. After solving a SimpleMembership issue that was in my Web.config I have the following error :
Login failed for user 'sa'
Anytime I try to reach a tab (on my page) that requires SQL Data display.
Here is my connectionStrings (the relevant one being DefaultConnection) :
<connectionStrings>
<add name="DefaultConnection" connectionString="packet size=4096;Data Source=MMSDEVNEW\SQL2008;Initial Catalog=CRAV34;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
<add name="CRAV34Entities"
connectionString="metadata=res://*/Models.CRA.csdl|
res://*/Models.CRA.ssdl|
res://*/Models.CRA.msl;
provider=System.Data.SqlClient;
provider connection string="data source=MMSDEVNEW\SQL2008;
initial catalog=CRAV34;
persist security info=True;
user id=sa;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
I read that this may be a permission issue or changing the security properties from the SQL Server Management Studio to "SQL Server and Windows Authentication mode". All of this has been done. The login and password are valid in the connectionString.
What could possibly cause this issue?
Just add Integrated security=true in your connection string
<add name="DbConnectionString" connectionString="Data Source=LENOVO\SQL2K12;Initial Catalog=EFCodeFirst;User ID=sa; Password=xxxxx;Integrated security=true" providerName="System.Data.SqlClient" />
If you are sure the SQL Server is in "mixed mode", and that the password is correct for the "sa" account, I can only suggest it to be that the "sa" user doesn't have access to the named database.
In SQL Management Studio, expand the database branch, then security, then users, and check if the "sa" user appears in the list.
The best way to diagnose this error is to try and connect to the database server using SQL Management studio, logging in AS the user in the connection string.
If you can log in, see if you can see your target database in the tree on the left.
If you can, try and open the database node and see if you can see all the tables.
If you can, see if you can run a select query against one of those tables.
If you can do ALL of the above, then it's NOT an issue with SQL server, but rather your connection string.
However, I've never seen that error come up when it's NOT a SQL configuration issue.
Try it with SQL Management Studio from the same machine you're code is running and it should give you more information about what's wrong.
Edit your post / reply to this answer if this doesn't solve it, explain what you tried and what the result(s) were, and I'll update the answer to reflect what I'd try next!
edit 1:
Try changing your connection string to the following:
packet size=4096;Server=MMSDEVNEW\SQL2008;Database=CRAV34;User ID=sa;Password=*****;
edit 2:
If this connection string is valid, it could actually be the second one that is causing a problem, as EF would likely choose that one unless explicitly told to use the "default" one.
As this second connection string doesn't have a password specified, it might be this that's causing the error.
Can you try it again with a password specified?
If you are using Integrated security=true, it could be the account you are running under does not have permission.
Try opening Visual Studio running as a different User. ( the user has permission on the database).
Use Shift and right click on the pinned Visual Studio menu.

A network-releated or instance-specific error occurred while establishing a connection to SQL Server

I have a simple mvc web site (using the VS internet template) on azurewebsites talking to a SQL Azure database in the same data center. The database at this time is just to do the built-in SimpleMembership Provider. I have already switched from the default App_Data mdf file to Azure SQL. It works fine, but sometimes after a while, it would give:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
with a long message:
SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance
using a database location within the application's App_Data directory.
The provider attempted to automatically create the application
services database because the provider determined that the database
does not exist. The following configuration requirements are necessary
to successfully check for existence of the application services
database and automatically create the application services database:
But I do not have a connection string using SQL Server Express!
Restarting the web site immediately does not remove the error.
Without changing anything and restarting the web site 15 minutes later give:
502 - Web server received an invalid response while acting as a
gateway or proxy server.
There is a problem with the page you are looking for, and it cannot be
displayed. When the Web server (while acting as a gateway or proxy)
contacted the upstream content server, it received an invalid response
from the content server.
Previously, I could restore the website back to working order by just re-publshing from VS. But for the last hour I have tried and tried and I cannot get the web site to work again.
What is the problem about SQL Server Express really about?
My connection string section contains:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=tcp:sabl6h4---.database.windows.net,1433;Database=MVC;User ID=test#sabl6h4---;Password=----;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
<add name="Entities" connectionString="metadata=res://*/Data.Model1.csdl|res://*/Data.Model1.ssdl|res://*/Data.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=sabl6h4---.database.windows.net;initial catalog=MVC;user id=test;password=----;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Just to be sure, I copied and pasted sabl6h4---.database.windows.net into SSMS and connected using login test and it opened the database without problem.
One day later: I created a new azurewebsite and published the exact same project and same web.config. It works fine without problems. After some minutes, the exact same error occurred!
Three more hours later: Went for lunch, came back, hit the browser Refresh button, and the web site is up again. I am convinced that someone is fooling around with the settings at the back end.
Add a <clear/> element as the first element under the connectionstrings element to ensure no strings are being inherited. Also, I assume your Azure SQL DB is set up with proper firewall settings allowing your IP where the ASP.net code is deployed?
I also had this error. But in my case I did not notice that the connection string was being overwritten by the publish wizard. I fixed the problem by going to the Settings tab in the Publish wizard and unchecking "Use this connection string at runtime (update destination web.config)"

What am I doing wrong with my deployed connection string?

I’m having a problem with my connection string after deploying. My site, www.coopersystems.net, is hosted by Godaddy. I’m using Visual Web Developer Express 2012 using E-Code First. Godaddy gave me the following connection string information:
Data Source=MyDatabaseName.db.10000000.hostedresource.com; Initial Catalog=MyDatabaseName; User ID=MyDatabaseName; Password='MyDatabasePassword';
I then used this info to change my connection string to the following:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=MyDatabaseName.db. 10000000.hostedresource.com; Initial Catalog = MyDatabaseName; User ID=MyDatabaseName; Password=’MyDayabasePassword’;Integrated Security=True" providerName="System.Data.SqlClient" />
<remove name="LocalSqlServer"/>
<add name="ApplicationServices" connectionString="Data Source=MyDatabaseName.db. 10000000.hostedresource.com; Initial Catalog = MyDatabaseName; User ID=MyDatabaseName; Password='MyDatabasePassword';Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The default page shows up fine but clicking on the tabs that should load other pages does not work. The page seems to hang for a while then the error page shows with error: “We’re sorry, we’ve hit an unexpected error. Click here it you’d like to go back and try again”.
My first question is:
1: If my connection string is wrong, would my default page still load? I ask this because when I tried changing keyword/value the home page would not load.
2: Is there something other than my connection string that I may be able to check?
3: How can I solve this problem?
Any suggestions or help would be greatly appreciated. I’m going on my second week of trying to get Godaddy to help and using their forums. Nothing has worked yet.
Latest attempts:
So I fixed the password quotes and had godaddy configured the config file to show the error message..
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I assume the server not found to mean that I provided the wrong name for a server on the Web Publish wizard so I’ve been trying different combinations of Server Name, Site Path, and Destination URL.. I got the site deployed with a few combinations of those values but the links still wont work.
Is there a configuration that maybe I should’ve done before using the “Publish” function of Web Developer Express 2012? I am out of ideas and Godaddy can’t help.
After one week of trying I decided to try to connect to the database using IIS 7.5. I can't connect with this either so I guess If I can solve connecting to the site or database with IIS I should be able to solve my other problems. Please help if you can. Thanks.
Try removing the single quotes around the password:
Password=MyDatabasePassword
Turns out that my ISP is blocking port 1433. No mater what I do I can't access the database.

MVC 4 Entity Framework Web config

I have an application which connects to our SQL Server 2008 enterprise.
My config file contains:
name="patbase" connectionString="Data Source=pbsqlserver1;Initial Catalog=patentbase;Trusted_Connection=True;;Application Name=PatBase Images;" providerName="System.Data.SqlClient"
When I test it from my PC it works.
When I publish it to the (internal) server I can see my user has logged into asp.net but data base access gives me:
An error occurred while getting provider information from the
database. This can be caused by Entity Framework using an incorrect
connection string. Check the inner exceptions for details and ensure
that the connection string is correct.
Your connection string is using a "trusted connection" which means the identity of the client process/thread is used in the SQL login attempt. You say that your user has logged in to the ASP.NET app, but what is the identity of the application pool your app runs in?
You don't say what kind of authentication your app uses, but I'm going to assume Windows Authentication. If you want the end-user's account to be used for the SQL login, you will need to enable ASP.NET impersonation for your app. You can do this under "Authentication" in the IIS Manager, or in your web.config with <identity impersonate="true" />
If you don't care which identity is used for the SQL login attempt, you can create a SQL login for your application pool's identity.
try to see if SSPI security works
<connectionStrings>
<add name="patbase" connectionString="Data Source=pbsqlserver1;Initial Catalog=patentbase;Integrated Security=SSPI;;Application Name=PatBase Images" providerName="System.Data.SqlClient"/>
</connectionStrings>
or remove the ;;Application Name=PatBase Images at the end if that doesn't work.

Resources