MVC 4 Entity Framework Web config - asp.net-mvc

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.

Related

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.

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.

Can't see the database used by the default MVC Account Controller/ASP.NET Membership provider

I fired up a new Internet application type MVC 3 project. It has an Account Controller that works using the default ASP.NET membership provider.
The web.config of the project has the following connection string:
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
I do have SQLEXPRESS on my machine, but when I look up the databases in the Management Studio, I do not see any database created for membership. I only see the ones that I created.
Where does ASP.NET MVC create this membership database?
You are using a User Instance (contained the fileaspnetdb.mdf in your SQL installations' data directory) not an embedded database within SQLExpress, which is why you don't see it in SSMS.
Here's an explanation of what this is and how to interrogate the data within it, should you need to.

Receiving an error doing code first MVC web deployment

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.

Windows Authentication issue with IIS and Asp.net MVC Intranet Application

So I've been working on an MVC app that pulls data from an existing database. I designed this as an 'Intranet Application' so that users would automatically be logged on to the app using their Windows credentials. This works, since the property #User.Identity.Name returns my Windows login name. (as well as other users), and they have access to the application itself. HOWEVER, when trying to access the database the information is on, I get the following error:
[SqlException (0x80131904): Login failed for user '**My computer name**'.]
It would appear that it's recognizing my windows login, but for some reason trying to use my machine name to access the database instead. At the company, this particular database is also accessed using employees' windows usernames, so I'd rather people be able to automatically access it using the built-in windows authentication in the MVC app.
In my web.config, I have identity impersonate set to false, authentication mode equal to Windows and the following keys in appSettings:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="enableSimpleMembership" value="false"/>
<add key="autoFormsAuthentication" value="false" />
The connection strings were auto-generated for me, and the information IS retrievable when running the app from Visual Studio, so I can only assume it's an issue with IIS settings. (I've never deployed an MVC app before)
On the application's IIS settings I have every authentication disabled except for Windows Authentication. Any ideas what I'm doing wrong? Let me know if there's any additional information you need as well.
Turn on the impersonation in your authentication configuration. Otherwise, the application pool's identity is used to connect to your Sql Server.

Resources