MySql ASP.NET Identity unexpectedly binding to SQL Server under Win10 - mysql

I have a live web application (ASP.NET 4.5.1, MVC 5.2.3, EF 6.1, MySql 5.1.35) which has worked for months without difficulty hosted on MS Server2008R2(Web).
I am attempting to move my dev environment to a Win10 virtual machine (previously a Win7 VM) and am opening a copy of the live system and replicated DB on the new machine in VS2015.
When running the application (I am using asp.net Identity 1.0 for authentication) I get the following exception in my _layout.vbhtml page:
An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Web.dll but was not handled in user code
Additional information: 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)
at:
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav" style="width:auto;white-space: nowrap;">
#If (Request.IsAuthenticated) Then
#<li>#Html.ActionLink("Home", "Index", New With {.Area = "", .controller = "Home"})</li>
If **User.IsInRole("Salesman")** Then
It's not surprising that the exception isn't being handled as I'm not using SQLServer...
I'm baffled why the User object (System.Web.Security.RolePrincipal) is using SQLClient rather than MySQLClient as specified:
Public Class ApplicationDbContext
Inherits IdentityDbContext(Of ApplicationUser)
Public Sub New()
MyBase.New("IMSSecurityEntities")
End Sub
Web.config:
<connectionStrings>
<add name="IMSSecurityEntities" connectionString="data source=localhost;database=db1;Uid=root;Pwd=password;" providerName="mysql.data.mysqlclient" />
The very same project when run in VS2015 on the Server2008R2 machine correctly connects to the MySQL db.
Would asp.net Identity maybe 'fall back' to using SQLServer in certain situations?

Did some more digging and googling and found a solution at: User.IsInRole doesn't work
Adding the following to web.config resolved the issue:
<modules>
<remove name="RoleManager" />
</modules>
RoleManager was defaulting to SimpleMembership. I don't know why this wasn't a problem on one machine but it was on the other but clearly there was ambiguity somewhere and this sorted it.

Related

AppHarbor: Connecting to a MySQL DB via ASP.NET MVC 5 + Dapper

I'm trying to get an application up and running on AppHarbor. It's been smooth sailing for the most part, but today I'm having trouble connecting to my MySQL database.
Using the credentials AppHarbor provided me with, I successfully connected to the mySQL database instance using dbForge Studio Express, created the necessary SQL schema, and populated the tables with data. Now I'm trying to use the following C# code to do a simple query using Dapper:
var sqlConnStringBuilder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["AppHarborMySql"].ConnectionString);
using (var db = new SqlConnection(sqlConnStringBuilder.ConnectionString))
{
db.Open();
// Do database query stuff
return result;
}
But this is the error that I'm running into, when I try to open the database connection:
An exception of type 'System.Data.SqlClient.SqlException' occurred in
System.Data.dll but was not handled in user code
Additional information: 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)
This is how I've declared the connection string in Web.config:
<connectionStrings>
<add name="AppHarborMySql" connectionString="server=MYDOMAIN.mysql.sequelizer.com;database=MYDBNAME;uid=MYUSERNAME;pwd=MYPASSWORD" providerName="System.Data.SqlClient" />
</connectionStrings>
Am I missing something obvious? I'm using ASP.NET MVC 5.x and the latest version of Dapper... thanks in advance!
And of course, despite having spent the better part of an hour investigating, I discover the answer immediately after posting the question.
For anyone else who may run across the same problem: I added the MySql.Web NuGet package to my MVC project, added using MySql.Data.MySqlClient; to the top of my page, and then changed my code to point from SqlConnection to MySqlConnection. Here's what my working code block now looks like:
using (var db = new MySqlConnection(ConfigurationManager.ConnectionStrings["AppHarborMySql"].ConnectionString))
{
db.Open();
// Do database query stuff
return result;
}

use of universal providers and mysql

I created an asp.net mvc 4 application, and added universal providers, because I need to create a custom membership provider that read users data from a my sql database.
When I work on the developer workstation it's all ok, but when I deploy on the server, where there is not Sql Server installed i receive an error. trying to open DefaultConnection.
In the InitializeSimpleMembershipAttribute that is created from the wizard, I have this:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: false);
this call initialize the db connection, do nothing, and is not used by the custom membership provider, but must be done at startup.
the connection string is:
add name="DefaultConnection" connectionString="Data Source=...;Initial Catalog=...;User id=...;Password=..." providerName="System.Data.SqlClient" />
the problem is that I don't know how to configure to use a mysql db and pass this control in the startup of the application.
If I have to install it in a server with Sql server installed, I configure it properly, and after the initial call the mysql membership provider works without problem, but i have problems when i cannot use this workaround, and exception is raised at the call to InitializeDatabaseConnection.
How can I fix this problem?

Entity Framework with MySql Exception - "The underlying provider failed on Open." - "Reading from the stream has failed."

We have an application that uses .NET Entity Framework 4 to connect to MySql. I've now set this up on several development machines (Windows 7) and am now deploying to Windows 2008 R2, but an exception is thrown when trying to access the database from the application.
There are two connection strings, one for use with ADO.NET and one for Entity Framework.
We are using MySql connector for .NET v 6.4.4
The exception is:
"The underlying provider failed on Open." - "Reading from the stream
has failed."
When googling the exception, most posts relate to the use of Entity Framework with SqlServer, and in these cases the problem looks to be authentication related. Since we are connecting here with the root user credentials, If this is an authentication issue, I'm not sure how to diagnose. Other applications connect to the database with the same credentials.
The connection strings are:
<add name="AppEntities" connectionString="metadata=res://*/OrmApp.AppEntities.csdl|res://*/OrmApp.AppEntities.ssdl|res://*/OrmApp.AppEntities.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;User Id=root;password=*********;Persist Security Info=True;database=app_staging;Convert Zero Datetime=True"" providerName="System.Data.EntityClient" />
<add name="AppAdoNet" connectionString="server=localhost;User Id=root;password=*********;Persist Security Info=True;database=app_staging;Convert Zero Datetime=True" providerName="System.Data.MySql" />
If anybody has experience of such an issue on MySql or can advise on how to diagnose the issue which I am presuming is at the database end, that would be appreciated.
Strange issue in the end. Answered here http://www.randombytes.me/2012/06/mysql-connector-net-reading-from-stream.html
Updated post link.
Summary:
The production server was using IIS setting 'Enable 32 bit applications' set to 'True', reverting to 'False' resolved the issue. Obviously you'll need to to check if another component in your application requires this flag set 'True'.

Entity Framework 4.1 The provider did not return a ProviderManifestToken string Exception

I downloaded the Asp.NET MVC 3 EntityFramework Code first sample application in vb.net and modified the connection string as I don't have SQL Server Express Edition to try it with Sql Server 2008 Web Edition
<add name="ApplicationServices"
connectionString="Server=MyPC\Sql2008;Trusted_Connection=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
when I try to run the application then ProviderIncompatibleException exception is thrown. And following error is displayed in the browser window.
Server Error in '/' Application.
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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)
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: 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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)
Source Error:
Line 24:
Line 25:
Line 26: #For Each item In Model
Line 27: #
Line 28:
Source File: C:\Documents and Settings\Shishir Shukla\My Documents\Downloads\CodeFirstEFVB\CodeFirstMVC\Views\Blog\Index.vbhtml Line: 26
The connection string which I have used works perfectly with my asp.net webforms application and also Winforms application.
Please help me soon to get rid of this problem as I have just switched to MVC.
The problem was with the connection string , I changed it to this and it worked ..
<add name="DBCon" connectionString="Data Source=.\SQL2008;Initial Catalog=TestDB;Integrated Security=True" providerName="System.Data.SqlClient" />
Can you provide a link to the project you downloaded?
You are using Windows Authentication, does the IIS account have permissions to the db?
Finally, verify your dbContext class is named "ApplicationServices". Otherwise, you may need to use the constructor that takes in a nameOrConnectionString.
Your connection string is a valid SQL connection string, but you need an Entity Framework connection string.
When you create your EF Model, create a new connection string from scratch. That will resolve this issue.
You can look at the new conneciton in your web.config, and you wil see that an Entity connection contains additional metadata as part of the connection string, and wraps the SQL conneciton string.
UPDATE:
A valid Entity connection string will look something like this:
<add name="MyDatabaseEntities" connectionString="metadata=res://*/Models.MyDatabase.csdl|res://*/Models.MyDatabase.ssdl|res://*/Models.MyDatabase.msl;provider=System.Data.SqlClient;provider connection string="data source=SERVER;initial catalog=MyDatabase;persist security info=True;user id=userid;password=password;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
However, you should let Visual Studio build the connection string for you.

MVC 3 - After publishing to server - CREATE DATABASE permission denied in database 'master'

Setup
MVC 3 EF 4.1 Code First - Windows 7 Pro 64 Dev machine, Separate Win 7 Pro for host.
I started with a SQLCE4.0 database, and everything worked fine (database initializer created tables, populated, etc...).
Once I got everything working like I wanted, I scripted out my SqlCe DB, created a real SQL 2008 DB, opened the proper ports and enabled TCP connections.
Problem
IF I Change my connection string to point to my intranet server\instance name, with a user I created for this app specifically, it works perfectly running from my dev machine.
IF I publish the app to the server (the web server and sql server are the same), and I try to login to the website (aka access the db), then I get:
CREATE DATABASE permission denied in database 'master'.
I commented out the DBInitializer setup so nothing should be created upon a db hit. The tables are there already.
If I do Database.SetInitializer(null); then I get a different login error:
I don't remember exactly but it was something to the affect of LOGIN FAILED FOR IIS APPPOOL\ApplicationPoolIdentity
So I changed the Identity to NETWORK SERVICE and even tried LOCAL SERVICE, both giving the same login failed error.
I tried following this post who says add a SQL user named 'IIS APPPOOL\MyApplicationName'. SQL Wont let me do that as it has invalid characters ... go figure.
Why does it work perfectly over the network accessing the DB from my dev machine, but when I publish to THE SAME machine that hosts the sql and the site, I get permission denied or login failed errors? I've scoured the net and tried different things.
EDIT: Here are my connection strings:
<add name="AntripContext" connectionString="Data Source=|DataDirectory|AntripWeb.sdf" providerName="System.Data.SqlServerCe.4.0" />
<!--<add name="AntripSQL" connectionString="Data Source=.\sqlexpress;Initial Catalog=Antrip;User Id=myuser;Password=mypassword;" providerName="System.Data.SqlClient" />-->
<add name="AntripWebEntities" connectionString="metadata=res://*/DAL.AntripEntities.csdl|res://*/DAL.AntripEntities.ssdl|res://*/DAL.AntripEntities.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="Data Source=|DataDirectory|\AntripWeb.sdf"" providerName="System.Data.EntityClient" />
<!--<add name="AntripWebEntities" connectionString="metadata=res://*/DAL.AntripEntities.csdl|res://*/DAL.AntripEntities.ssdl|res://*/DAL.AntripEntities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\sqlexpress;Initial Catalog=Antrip;User Id=myuser;Password=mypassword;"" providerName="System.Data.EntityClient" />-->
Note: The commented out connection strings are the ones that use the sql 2008 database (that break my published app).
It sounds to me as if your connection string has Integrated Security=true so that you are connecting to SQL as the user you specify, rather than the SQL user you think you are using?
But beware, its still trying to create a database: you need to swap the IDatabaseInitializer - I guess it will look for a DB named the same as the entitys to be careful!