asp.net mvc 3 membership, cannot connect to database - asp.net-mvc-3

I have installed visual studio 2010 as well as mvc 3 and have been working on a site.
Later I installed sql server 2008 and when I tried to register a user through provided membership in asp.net mvc i had bunch of errors saying that there was no data base or something like that, so through a lot of research i finally added the aspnetdb to my sql server and enabled remote accessing... and as of right now when i try to register i get this error: Unable to connect to SQL Server database.
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)
this is my connection string:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
I remember that some time ago working on my previous projects i had no problem with the membership provider and registering users loging in and so on.. so i'm really frustrated and don't know what i'm missing here.

You can use it directly from localhost, no need to put it in App_Data folder. Use this connection string:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=*Name of your database*;Integrated Security=SSPI"/>
</connectionStrings>

Related

Membership.GetAllUsers() System.Data.SqlClient.SqlException: User does not have permission to perform this action

I created a small web application using Visual Web Developer 2010 Express. After I deployed my site and go to the first page, which is the login page that provides a list of users for the app, I get this error:
Exception Details: System.Data.SqlClient.SqlException: User does not have permission to perform this action.
Source Error:
Line 22: public ActionResult LogOn()
Line 23: {
Line 24: var users = Membership.GetAllUsers().Cast<MembershipUser>().ToList();
Line 25: SelectList usernames = new SelectList(users);
Line 26: ViewBag.UserNames = usernames;
Here is my connectionStrings settings:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
Why is this error occurring? Any help is much appreciated. I'm currently developing it on a Windows 2008 Server and testing the deployment of it on the same machine.
I found the issue with my connection string. It was set up for user instancing, which according to this Microsoft article:
Web applications running on IIS 7.5 and that rely on SQL Server
Express user instancing will fail to run using the default IIS 7.5
security configuration on both Windows 7 Client and Windows Server
2008 R2
The article also recommends avoiding usage of user instances in production. I found this post which helped me host the database in a normal .\SQLExpress instance. I changed my connection strings to:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Database="app_users";Data Source=.\SQLEXPRESS;Initial Catalog=app_users;Integrated Security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Before changing the above connection string, I attached the aspnetdb.mdf file in SQL Server Express to a new database I created called "app_users".
I no longer get the permissions error.

Connect MVC 3 application to sql server 2008

I am newbie and confused how to do it.
I have built MVC 3 application in Visual Studio 2010 and published to the localhost and it's working fine.
Here, What I actually want is to use the Sql Server 2008 installed in my computer rather than database under the folder APP_DATA that I have created using visual studio.
I am new to the MVC and don't know about the database connection detailly. Following is the connection string of the web.config file:
<connectionStrings>
<add name="IVRControlPanelEntities" connectionString="metadata=res://*/Models.IVRControlPanelModel.csdl|res://*/Models.IVRControlPanelModel.ssdl|res://*/Models.IVRControlPanelModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\IVRControlPanelDB.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
This is the connectionstring automatically added by the visual studio while adding the data entity model.
After published to localhost the database used by mvc 3 is under the APP_DATA folder of wwwroot folder.
What I need to change to connect to the Sql Server 2008 installed in my computer rather than database created on the Visual Studio.
One more thing, I could not import the database created by visual studio to the sql server 2008 which is in the form IVRControlPanelDB.mdf
You should use Database.SetInitializer<> method in Application_Start method in Global.asax
read this and this ,this article may help too.
You need to modify your connectionstring to add the address to your sql server. Example:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Server=localhost;Database=myDatabase;User Id=myUser;Password=myPassword;"
providerName="System.Data.SqlClient" />
</connectionStrings>

MVC app deployed on Azure cannot connect to SQL Azure Database but can connect via SSMS

I have an MVC 3 app using Entity Framework that was happily hosted in Azure and talking to SQL Azure Database 2 days ago. I then had a minor schema change to the database and so dropped and re-created the database via SSMS and also published the application again as I had added the default MVC app login role controls to the site - since then I cannot get my app to connect to the database and get the following error logged in an Elmah xml file:
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)" source=".Net SqlClient Data Provider" detail="System.Data.EntityException
I can connect to the SQL Azure Database using SSMS and when I run my application in the Azure emulator connected to the SQL Azure Database everything runs perfectly but once published it refuses to connect. Any help would be massively appreciated!
A SQL Azure connection string (using EF Model First / Database First) typically lookes like this:
<connectionStrings>
<add name="SampleEntities" connectionString="metadata=res://*/Models.SampleEntities.csdl|res://*/Models.SampleEntities.ssdl|res://*/Models.SampleEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=tcp:abcdefg.database.windows.net,1433;Database=asdf;User ID=manager#abcdefg;Password=XXXXXX;Trusted_Connection=False;MultipleActiveResultSets=True;Encrypt=True;"" providerName="System.Data.EntityClient" />
</connectionStrings>
A SQL Azure Connection string (using Code First) typically looks like this:
<connectionStrings>
<add name="SampleContext" connectionString="Data Source=tcp:abcdefg.database.windows.net,1433;Database=asdf;User ID=manager#abcdefg;Password=XXXXXX;Trusted_Connection=False;MultipleActiveResultSets=True;Encrypt=True" providerName="System.Data.SqlClient" />
</connectionStrings>
The second connection string will also work without Entity Framework.
I believe you've published a config to Azure that does not have a correct SQL Azure server name, but rather some local/on-premise SQL server. Could this be it? Can you remote into the instance and validate correct .config and SQL Azure server?
From the error it appears that your app is not using TCP/IP to connect to your SQL Azure, but rather Named Pipes, which usually implies that the SQL Azure server name is not likely a valid TCP/IP host.

System.Data.SqlClient.SqlException: Failed to generate a user instance of SQL Server due to a failure in impersonating the client

i have published my asp.net MVC 3 web application locally using IIS 7 express and visual developer express 2010, but when i tired to login to the application it returned the following error:-
System.Data.SqlClient.SqlException: Failed to generate a user instance of SQL Server due to a failure in impersonating the client. The connection will be closed.
so what might be causing this problme?
BR
Edit:-
the current connection string is :-
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="ElearningEntities" connectionString="metadata=res://*/Models.Elearning.csdl|res://*/Models.Elearning.ssdl|res://*/Models.Elearning.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MScProject.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
If you have enabled SSPI in your connection string, you might no longer be able to perform it when the SQL server is on a separate tier than your web server. You could use standard SQL Server authentication with a username and password in this case.

MVC3 out of the box membership provider (log on) not working on newly published server?

I just published my mvc3 application that uses the default membership provider to login but it seems that the call to anything "Membership" (Membership.ValidateUser-in this case) is causing the following error on the remote server hosting my web application.
Server is 2003 SP2 IIS6.0...my mvc3 application hits sql server 2008 instance.
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)
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.
SQLExpress database file auto-creation error:
How is the out of the out of the box (new mvc3 app) user/login managed with MVC3 and how can I set this user store up on my remote IIS server?
By default it creates a connection to a local SQL Express db in your App_Data folder the first time you run the mvc web app and register a user. You will then notice the aspnetdb file is created.
If you wish to use your existing db you need to create the Membership schema on an existing database.
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
Easiest way is to update the web.config connection string to point to new db and run the install scripts agaist this.
These scripts are located in the WINDIR%\Microsoft.Net\Framework\v2.0.50727\ folder with names like InstallCommon.sql, InstallMembership.sql, InstallRoles.sql, InstallProfile.sql, InstallSqlState.sql, and so on.
See this more in depth article for full overview
http://www.asp.net/security/tutorials/creating-the-membership-schema-in-sql-server-cs

Resources