Using membership provider in MVC2 with MySQL - mysql

I've read several posts in this forum about this but nothing seems to work. I'm trying to replace the default SQL Server based providers with MySQL providers using the latest version of connector (6.3.6.0) and VS2010, but I keep getting an error when accessing the Security section in WSAT. Here are my steps:
1) create a new mysql database.
2) create a new MVC2 application.
3) change web.config as follows:
<connectionStrings>
<remove name="LocalMySqlServer"/>
<add name="LocalMySqlServer"
connectionString="Data Source=127.0.0.1;Port=3306;Database=Sample;User id=root;Password=mysql;"
providerName="MySql.Data.MySqlClient"/>
<remove name="ApplicationServices"/>
<add name="ApplicationServices"
connectionString="Data Source=127.0.0.1;Port=3306;Database=Sample;User id=root;Password=mysql;"
providerName="MySql.Data.MySqlClient" />
</connectionStrings>
...
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"
connectionStringName="MySqlMembershipConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"
autogenerateschema="true"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
name="MySqlProfileProvider"
applicationName="/"
connectionStringName="MySqlMembershipConnection"
autogenerateschema="true"/>
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add connectionStringName="MySqlMembershipConnection"
applicationName="/"
name="MySqlRoleProvider"
type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"
autogenerateschema="true"/>
</providers>
</roleManager>
<machineKey validationKey="AutoGenerate" validation="SHA1"/>
When I run WSAT and click Security, I get this error:
There is a problem with your selected data store. This can be
caused by an invalid server name or
credentials, or by insufficient
permission. It can also be caused by
the role manager feature not being
enabled. Click the button below to be
redirected to a page where you can
choose a new data store.
The following message may help in
diagnosing the problem: The source was
not found, but some or all event logs
could not be searched. To create the
source, you need permission to read
all event logs to make sure that the
new source name is unique.
Inaccessible logs: Security.
Could anyone tell me what's wrong with this procedure? Thanks to all!

finally I got it working and I'd like to share the information here. I am using the latest version of Connector/Net (6.3.6) in an ASP.NET MVC 2 website. Here is what I did:
1) create a new MySql database (or just use yours if any).
2) ensure that your machine.config has enabled schema autogeneration for MySQLMembershpProvider. As you probably know, machine.config is typically placed under c:\windows\microsoft.net\framework[version]\config\machine.config. Find under the entry for MySQLMembershipProvider and append the attribute autogenerateschema="true" to the element . The whole element will look like this:
<add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" autogenerateschema="true"/>
3) in your web.config (I'm referring to a newly created web application, so make your changes if required) change the connection strings and providers so that they refer to MySql. Typically: under connectionStrings add:
<remove name="ApplicationServices"/>
<add name="ApplicationServices" connectionString=connectionString="server=YOURSERVER;UserId=YOURUSER;password=YOURPASSWORD;Persist Security Info=True;database=YOURDATABASE;charset=utf8" providerName="MySql.Data.MySqlClient"/>
(BTW, notice the charset=utf8 in the connection string: this is not required for membership, but it is required if you are going to use this connection string to exchange Unicode data: it is not enough to just define the character set in your MySql table fields!).
Also, under membership / providers... ensure that the membership provider element (typically something like add name="AspNetSqlMembershipProvider"...) connectionStringName attribute refers to the connection string you set above (in my case, connectionStringName="ApplicationServices"). Do the same for profile and roleManager providers, which should look like this:
<profile>
<providers>
<clear/>
<add type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
name="AspNetSqlProfileProvider"
applicationName="/"
connectionStringName="ApplicationServices"
autogenerateschema="true"/>
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add connectionStringName="ApplicationServices"
applicationName="/"
name="MySqlRoleProvider"
type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"
autogenerateschema="true"/>
</providers>
</roleManager>
<machineKey validationKey="AutoGenerate" validation="SHA1"/>
(Note the SHA1 validation!).
4) launch WSAT from Visual Studio and let it configure your MySql database. It should create all the tables required and let you create users and assign roles. Beware of table names casing! With WSAT tables like myaspnet_Users (not myaspnet_users) are created, and this might lead to confusions (MySQL Workbench seems to be confused by this and will show you just 1 table if you have 2 tables with their names differing only by casing. Tools like Navicat seem smarter in this respect). It's best to let WSAT create its tables to avoid this fuss.
Finally, remember that if you are going to use my_aspnet... tables in some relationships, you must make sure you select the proper MySQL database engine, i.e. not MyISAM but InnoDB, otherwise your foreign keys even if set will be ignored.
That's all I can share for this issue. Hope this might save some hours to someone else...

Perhaps I found the culprit: the PC I was working today had not its machine.config (for .net 4) configuration set for autogenerateschema="true" (btw there is a typo in my code above, the connection string name is not correct, but this happened only by copy/paste in my post). So maybe this is useful for all the newcomers like me: remember to set autogenerateschema="true" in
Thanks anyway

Related

Membership and rolls through visual studios 2012 MySql

I am trying to set membership and rolls through visual studios 2012 using my live hosted MySQL database.
I have been following this tutorial .
After trying a few times, I am getting stuck on this one part where I run Web Site Administration Tool and keep getting the same error when I click on the security tab
error
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.
The following message may help in diagnosing the problem: Type is not resolved for member 'MySql.Data.MySqlClient.MySqlException,MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=******'.*
My code is following
machine.config code
<membership>
<providers>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=*******" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
<add name="MySQLMembershipProvider" autogenerateschema="true" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=***********" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
</providers>
</membership>
web.config code
<connectionStrings>
<remove name ="LocalMySqlServer"/>
<add name="LocalMySqlServer"
connectionString="Datasource=000.000.00.000;Database=******;uid=******;pwd=*******;"
providerName="MySql.Data.MySqlClient"/>
I am not sure of what is the cause of this error. What i am missing here?
If you can run your application - just do it and see actual database related error. As for me it was not existing database. I manually created the database and only after that went on with "Web Site Administration Tool" successfully.
And check connection string format. My looks like
Server=localhost;Database=xxxx;Uid=yyyy;Pwd=zzzz;
Hope this helps

Cannot connect to SQL server AppHarbor

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Is the above relevant? I just have it in there in order not to screw things further.
But below is what I'm interested in, it is a connection string of my local sql server
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=lews;Password='therin'" providerName="System.Data.SqlClient" />
"When you create a database on Sequelizer you can specify a connection string alias. This is done on the Sequelizer add-on page (follow the "Go to ..." link on the application overview). If you set this name as the name of your connection string in your configuration file, we'll automatically replace it with your Sequelizer connection string when your code is deployed."
That's a snippet from Appharbor's documentation. So I assume that Data Source, Initial Catalog, User ID and password is automatically replaced with the correct values by AppHarbor. But it can't connect for some reason.
Below is another string I am using with MySQL this time, again I assume that AppHarbor should automagically inject the right values, but the error it gives is:
"PeopleEntities cannot be found in the application configuration file"
What is going on?
<add name="PeopleEntities" connectionString="metadata=res://*/Context.People.csdl|res://*/Context.People.ssdl|res://*/Context.People.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;database=people"" providerName="System.Data.EntityClient" />
</connectionStrings>
Btw, the names "PeopleEntities" and "ApplicationServices" are used as aliases on AppHarbor.
And I have no idea how to use the code given in the documentation, databases is just not my thing.. how do I use both local and remote conn strings? Where in the code do I build the string and inject it? Do I have to do it whenever I create a DBContext instance? Etc..
Any ideas will be great, thanks!
EDIT:
Btw, if I hard code the connection strings, in the app.config and use a wcftestclient, it works, it queries the database.. but this isn't a good idea, apparently the connection strings can change without warning.
Anyway if I deploy it with the strings hardcoded and connect to the database with my site.. it doesn't query the SQL server.. really confused :(
http://support.appharbor.com/discussions/problems/2687-solved-mysql-provider-with-entity-framework-problem
For Entity Framework:
This piece of markup is unbelievable important:
<system.data>
<DbProviderFactories>
<clear />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,
Version=6.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
Oh make sure the MySQL connector assemblies are local. And it should be in your web.config file as well, to be extra sure place it in your WCF's App.config. Use the same addon alias in your connection string.
For SQL Server addon:
You need this markup in the web.config:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
</system.web>
And you should be good to go..

MySQL Forms Authentication Hashed password problem

I am trying to use the ASP.NET forms authentication service with the MySQL connector version 6.3.2. I was able to get it working using cleartext passwords but unable to get hashed passwords working. Here is a snippet from my machine.config file
<system.web>
<membership defaultProvider="MySQLMembershipProvider">
<providers>
<add name="MySQLMembershipProvider"
type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.3.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
autogenerateschema="true"
connectionStringName="LocalMySqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
applicationName="/"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="1"
passwordAttemptWindow="10"
passwordStrengthRegularExpression="" />
</providers>
</membership>
</system.web>
I am using the ValidateUser method of the MembershipProvider class to perform authentication. If the line passwordFormat="Hashed" is changed to passwordFormat="Clear" users will authenticate. After changing settings in the machine.config file I remove all users and recreate the accounts. I inspected the contents of the aspnet tables and the passwords are being stored properly as hashes - just failing to verify.
Looks like I'm not the only one who has noticed this bug: http://forums.mysql.com/read.php?38,368612,372250. Easily fixed by adding the following to the web.config:
<system.web>
<machineKey validationKey="AutoGenerate" validation="SHA1" />
</system.web>

Unable to initialize provider. Missing or incorrect schema. for MySql.Web connector

I am trying to use MySql Connector 6.2.2.0 for membership and role providers.
The issue I'm having is: Unable to initialize provider. Missing or incorrect schema.
<authentication mode="Forms"/>
<roleManager defaultProvider="MySqlRoleProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".ASPROLES"
cookieTimeout="30"
cookiePath="/"
cookieRequireSSL="false"
cookieSlidingExpiration="true"
cookieProtection="All" >
<providers>
<clear />
<add
name="MySqlRoleProvider"
type="MySql.Web.Security.MySQLRoleProvider, MySql.Web,
Version=6.2.2.0,Culture=neutral, PublicKeyToken=c5687fc88969c44d"
connectionStringName="mySQL"
applicationName="capcafe"
writeExceptionsToEventLog="true"
/>
</providers>
</roleManager>
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<add connectionStringName="mySQL"
applicationName="capcafe"
minRequiredPasswordLength="5"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
minRequiredNonalphanumericCharacters="0"
name="MySqlMembershipProvider"
type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.2.2.0,
Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</providers>
</membership>
Here is the line it doesn't seem to like:
Line 57: type="MySql.Web.Security.MySQLRoleProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
I have both MySql.Web and MySql.Data referenced and in my bin! Any help resolving this issue will be very much appreciated.
Add references to the assemblies, add autogenerateschema="true" attribute to both as:
<providers>
<remove name="MySQLProfileProvider"/>
<add name="MySQLProfileProvider" autogenerateschema="true" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/"/>
</providers>
use ASP.Net configuration tool.
I had this problem, it turned out there was no password in my connection string, I think checking carefully that your connection string is correct would be a good place to start.
My problem was I had "localhost" in my connection string instead of the IP address of the webhost's MySQL server.
Once I changed that in my web.config file it worked fine, so you need to check your web.config file very carefully.
I was experiencing this exact same issue. Mine ended up being a case issue since I was deploying my site to a linux server running Mono. Enabling autogenerateschema="true" helped me figure this one out. Some hosts won't let the code generate the necessary tables though, so if it doesn't auto-generate your schema then check out casing issues.
CodeMonkey's solution worked for me... I was actually deploying a new app to a Win 2008 Server VM. The schema could not be generated until I specified the LocalMySql connection string and set the MySQLRoleProvider autogenerate to true.

How do I setup ASP.NET MVC 2 with MySQL?

Is it possible to setup ASP.NET MVC 2 to work with a MySQL database?
I'm assuming that you have Visual Studio Professional 2008, have access to an instance of MySQL server, and have moderate to advanced development experience. This MAY work with VS2008 Web edition, but not at all sure.
If you haven't, install MySQL Connector for .NET (6.2.2.0 at the time of this write-up)
Optional: install MySQL GUI Tools
If you haven't, install MVC 2 RTM, or better yet, use Microsoft's Web Platform Installer. (UPDATE: MVC 2 has now been released for quite some time)
Create an empty MySQL database. If you don't want to access your application with the MySQL root user account (insecure), create a user account and assign the appropriate privileges (outside the scope of this write-up).
Create a new MVC 2 application in Visual Studio
In the MVC 2 app, reference MySql.Web.dll. It will either be in your GAC, or in the folder that the MySQL Connector installer put it.
Modify the connection strings portion of your web.config:
<connectionStrings>
<remove name="LocalMySqlServer"/>
<add name="MySqlMembershipConnection"
connectionString="Data Source=[MySql server host name];
userid=[user];
password=[password];
database=[database name];"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
8.
Modify the membership portion of your web.config:
<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider"
type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
connectionStringName="MySqlMembershipConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"
autogenerateschema="true"/>
</providers>
</membership>
9.
Modify the role manager portion of your web.config:
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add connectionStringName="MySqlMembershipConnection"
applicationName="/"
name="MySqlRoleProvider"
type="MySql.Web.Security.MySQLRoleProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
autogenerateschema="true"/>
</providers>
</roleManager>
10.
Modify the profile portion of your web.config:
<profile>
<providers>
<clear/>
<add type="MySql.Web.Security.MySQLProfileProvider, MySql.Web,
Version=6.2.2.0, Culture=neutral,
PublicKeyToken=c5687fc88969c44d"
name="MySqlProfileProvider"
applicationName="/"
connectionStringName="MySqlMembershipConnection"
autogenerateschema="true"/>
</providers>
</profile>
At this point, you ought to be able to run the app and have the default ASP.NET MVC 2 home page come up in your browser. However, it may be a better idea to first run the ASP.NET Web configuration Tool (in Visual Studio top menus: Project -> ASP.NET Configuration). Once the tool launches, check out each of the tabs; no errors = all good.
The configuration tool at Nathan Bridgewater's blog was essential to getting this working. Kudos, Nathan. Look for the "Configuration Tool" heading half way down the page.
The public key token on the MySql.web.dll that I've posted here ought not change any time soon. But in case you suspect a bad token string from copying and pasting or whatever, just use the Visual Studio command line to run: "sn -T [Path\to\your.dll]" in order to get the correct public key token.
There you have it, ASP.NET MVC 2 running over MySQL. Cheers!
I belive at "10. Modify the profile portion of your web.config::"
<profile>
<providers>
<clear /> ...
<add type="MySql.Web.Security.MySQLProfileProvider,......
type= has to be:
type="MySql.Web.Profile.MySQLProfileProvider"
because in "MySql.Web.Security" I have not found any method MySQLProfileProvider.
(but using Version 6.4.4. for .NET 4.0)
And at least, you have to create your own classes for creating the database tables, if there is no ready configured database.
Harald