Login failed for user 'sa' ASP.NET MVC page - asp.net-mvc

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.

Related

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.

Which connection string WSAT uses

I create a new ASP.NET MVC project with internet template.
I build the solution.
I open ASP.NET Web Site Administration Tool (WSAT).
I click the security tab
I get this error:
"Unable to connect to SQL Server database."
I am using Visual Studio 2012 and SQLServer Express 2012.
I don't have IIS installed (other then what ships with Visual Studio).
The Connection String from the new project:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication2-20130804051506;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication2-20130804051506.mdf" providerName="System.Data.SqlClient" />
It is worth mentioning that I did not change anything in the web.config after its initial creation and that MVC project seems to be working properly e.g. I am able to register and login and then see that data in the (LocalDb)\v11.0.
I read on other posts that the connection string from machine.config is sometimes used instead of the one from web.config.
I have a two machine.config files, one under C:\Windows\Microsoft.NET\Framework\config\v4.0.30319 and the other under C:\Windows\Microsoft.NET\Framework\CONFIG\v2.0.50727
When is a machine.config's connection string is used instead of my web.config's connection string?
I want to use the (LocalDb)\v11.0 data source, why does WSAT unable to connect to it?
From http://forums.asp.net/t/1483981.aspx/1 :
by default, the membership / role provider uses the "localSQLServer" connection string which is set to point to a local SQL Express database from the root web.config. In most cases, the server do not have SQL express installed and you will get this error.
By clearing the connection strings should reveal those errors.

Trouble deploying EF CodeFirst to server

I have a CodeFirst web application that originally used LocalDb. I changed the connection string to:
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SqlExpress;Initial Catalog=aspnet-BatchFindings;Integrated Security=True" />
And I created the appropriate database using Server explorer.
I am using WebDeploy to publish to a Windows Server 2012 instance running IIS8.
After publishing, I was unable to use the application; it did not have permissions for the database. I then changed the identity to a user account, and now I do not get database errors. Except the application does not appear to save any data to the database. I am at a complete loss - any help is appreciated!
Recap:
Entity Framework, Code First
Web Deploy to IIS 8
Custom Application Pool, identity set to a user account
Data not being stored in database.
EDIT: I would love to be able to simply use Web Deploy and have it use its own database (so it does not get test data from mine) but this is just so frustrating! I am publishing, the connection string should work, not sure where I am going wrong.
Stupid question but are you calling DbContext SaveChanges() method after data manipulation - data saving works when testing?
Also you can use different web.config files when testing (Debug) and deploying (Release version).
So I inadvertently figured out the reason why it was not saving. For some reason in my test environment everything worked, but when it was pushed out, I needed to add the following line:
db.Entry(obj).State = System.Data.EntityState.Added;
I checked out the tutorial, which seemed to work until it went into that Grant.sql file. There is no option in Visual Studio to connect.
What I ended up doing in that case to get the database working was set my Application Pool to run under a user identity I created for this purpose, while setting Load User Profile to True. You can also run as LocalSystem (as long as you create the following folder: C:\Windows\SysWOW64\config\systemprofile\Desktop - this tricks the system into thinking the LocalSystem account is interactive).

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