|
Introduction
This article provides two files that contain a Membership provider and a Role provider for ASP.NET v2.0.
Microsoft provides a Membership provider in the framework but only for SQL Server. The class does not seem to work for MySQL so I decided to write a new provider from the ODBC provider sample code included in the framework SDK.
How to use it
To use these classes, you will need the latest MySQL .NET Connector. You might need to recompile it with the new C# compiler for v2.0 (but the new framework should normally support v1.0 or 1.1 assemblies).
- Create the tables (note that these SQL commands might not work with MySQL 4).
CREATE TABLE Roles
(
Rolename Varchar (255) NOT NULL,
ApplicationName varchar (255) NOT NULL
)
CREATE TABLE UsersInRoles
(
Username Varchar (255) NOT NULL,
Rolename Varchar (255) NOT NULL,
ApplicationName Text (255) NOT NULL
)
ALTER TABLE 'usersinroles'
ADD INDEX ( 'Username', 'Rolename', 'ApplicationName') ;
ALTER TABLE 'roles' ADD INDEX ( 'Rolename' , 'ApplicationName' ) ;
CREATE TABLE 'users' (
'PKID' varchar(36) collate latin1_general_ci NOT NULL default '',
'Username' varchar(255) collate latin1_general_ci NOT NULL default '',
'ApplicationName' varchar(100)
collate latin1_general_ci NOT NULL default '',
'Email' varchar(100) collate latin1_general_ci NOT NULL default '',
'Comment' varchar(255) collate latin1_general_ci default NULL,
'Password' varchar(128) collate latin1_general_ci NOT NULL default '',
'PasswordQuestion' varchar(255) collate latin1_general_ci default NULL,
'PasswordAnswer' varchar(255) collate latin1_general_ci default NULL,
'IsApproved' tinyint(1) default NULL,
'LastActivityDate' datetime default NULL,
'LastLoginDate' datetime default NULL,
'LastPasswordChangedDate' datetime default NULL,
'CreationDate' datetime default NULL,
'IsOnLine' tinyint(1) default NULL,
'IsLockedOut' tinyint(1) default NULL,
'LastLockedOutDate' datetime default NULL,
'FailedPasswordAttemptCount' int(11) default NULL,
'FailedPasswordAttemptWindowStart' datetime default NULL,
'FailedPasswordAnswerAttemptCount' int(11) default NULL,
'FailedPasswordAnswerAttemptWindowStart' datetime default NULL,
PRIMARY KEY ('PKID')
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
- Change
MySqlMembershipProvider::encryptionKey to a random hexadecimal value of your choice.
- Upload the two files to ~/App_Code.
- Modify your web.config using the following template (if you are on a shared hosting server, you will have to set
writeExceptionsToEventLog to false): <connectionStrings>
<add name="ConnString"
connectionString="Database=YOURDBNAME;Data Source=localhost;
User Id=YOURUSERNAME;Password=YOURPWD" />
</connectionStrings>
<system.web>
<roleManager defaultProvider="MySqlRoleProvider"
enabled="true"
cacheRolesInCookie="true"
cookieName=".ASPROLES"
cookieTimeout="30"
cookiePath="/"
cookieRequireSSL="false"
cookieSlidingExpiration="true"
cookieProtection="All" >
<providers>
<clear />
<add
name="MySqlRoleProvider"
type="Andri.Web.MySqlRoleProvider"
connectionStringName="ConnString"
applicationName="YOURAPPNAME"
writeExceptionsToEventLog="true"
/>
</providers>
</roleManager>
<membership defaultProvider="MySqlMembershipProvider"
userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="MySqlMembershipProvider"
type="Andri.Web.MySqlMembershipProvider"
connectionStringName="ConnString"
applicationName="YOURAPPNAME"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="true"
passwordFormat="Hashed"
writeExceptionsToEventLog="true"
/>
</providers>
</membership>
</system.web>
- That's it! You can use the login controls provided in the ASP.NET framework v2.0 if you want or use your own controls.
You can do whatever you want with this code but just send me an email if you have done some improvements or some bug correction. Isn't that what this website is about, sharing?
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 193 (Total in Forum: 193) (Refresh) | FirstPrevNext |
|
|
 |
|
|
 |
|
|
Hello,
I get a "Could not find any recognizable digits" error when going to the next CreateUserWizard step. Anyone an idea why this happens? I've been cracking my head on it, but no luck...
Regards,
Joep
--
Server Error in '/' Application. --------------------------------------------------------------------------------
Could not find any recognizable digits. 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.FormatException: Could not find any recognizable digits.
Source Error:
Line 1536: byte[] returnBytes = new byte[hexString.Length / 2]; Line 1537: for (int i = 0; i < returnBytes.Length; i++) Line 1538: returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); Line 1539: return returnBytes; Line 1540: }
Source File: e:\linkedpapers.com\wwwroot\App_Code\MySQLMembershipProvider.cs Line: 1538
Stack Trace:
[FormatException: Could not find any recognizable digits.] System.ParseNumbers.StringToInt(String s, Int32 radix, Int32 flags, Int32* currPos) +0 System.Convert.ToByte(String value, Int32 fromBase) +41 Andri.Web.MySqlMembershipProvider.HexToByte(String hexString) in e:\linkedpapers.com\wwwroot\App_Code\MySQLMembershipProvider.cs:1538 Andri.Web.MySqlMembershipProvider.EncodePassword(String password) in e:\linkedpapers.com\wwwroot\App_Code\MySQLMembershipProvider.cs:1490 Andri.Web.MySqlMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status) in e:\linkedpapers.com\wwwroot\App_Code\MySQLMembershipProvider.cs:453 System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser() +305 System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e) +105 System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e) +453 System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e) +149 System.Web.UI.WebControls.WizardChildTable.OnBubbleEvent(Object source, EventArgs args) +17 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35 System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +115 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi
can someone pls tell me how to add custom fields and provide the updation using andris mysql membership provider
thanks
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
Hi I am having a problem on my login page with this provider. In the loggedIn event of my login control i am trying to get the currently logged in user with this code
Dim u As MembershipUser = Membership.GetUser
but i am getting the error object not set to instance of object
any help much appreciated thanks
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
i got the same problem, i found the problem with pApplicationName because the function use it, and in the context of that method it is null at this moment. i suppose that initialize method must set this variable when application start and invoke the class, but i dont know why the value of pApplicationName becomes null later....
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi all, i am using this cs files. work nicely. my last was about navigation error, but that because of tables. now all working fine. Here i am troubling of custom error messages. In change password, i dont want any construction for new messages. but password error or any error occured is shown as "Password incorrect or New Password invalid. New Password length minimum: 7. Non-alphanumeric characters required: 1.". I want to delete the second sentence. how i customize. any possibility
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi, i used MySQLProvider and MyRole Provider as well. All r fine. if any logn problem it Login control show the error. But after enter the correct username and password, Login Control shows as Logged In but didnt redirect to requested page. The same login page will show. Wat is the problem. any one help me. please
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
How change the time of the Cookies Time Expires to 5 days? All time I get the value: "#12:00:00 AM#"
Example from web.config:
<forms loginurl="login.aspx" name="MYWEBAPP.ASPXAUTH" protection="All" timeout="3000000" />
Help...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Is it possible to use this code for a oracle database. how to create a role provider for connecting to oracle database.
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
|
This article is VERY good; however, MySQL also has an ASP.NET Provider available for download from their site (MySQL Connector/Net 5.2.1).
I've documented my experiences with 5.2.1 (which is alpha at this time) at http://tinyurl.com/28qd33[link]
modified on Saturday, March 8, 2008 1:24 PM
|
| Sign In·View Thread·PermaLink | 2.67/5 (3 votes) |
|
|
|
 |
|
|
Using the method on your blog, I see the following error when I go to the ASP.NET Web Site Administration Tool provider tab, click “Select a Different Provider (advanced)” and test the MySQLMembershipProviderprovider:
Could not establish a connection to the database. If you have not yet created the SQL Server database, exit the Web Site Administration tool, use the aspnet_regsql command-line utility to create and configure the database, and then return to this tool to set the provider.
Any thoughts? I can't find any documentation on how to get the MySql Connection/Net Profile Provider anywhere...yours was the closest I found.
What are you coding as the provider type? "MySql.Data, Version=5.2.1.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"?
You say "After the tables are built, you’ll want to setup your web application’s web.config (using your machine.config as a template)"...can you be a bit more specific? Would you mind posting a full blown example?
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
I tried the example in this article and got most of the way through but ended up getting connection problems.
I tried the example from your link (to the blog) and it was quicker and easier. After a couple of configuration tweaks I was up and running.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I've spent days looking, and I keep getting this page! I've tried this code and found the same problems others have listed.
I'm using VWD 2008 Express, MySQL 5.0, and .NET 2.0. I cannot be alone in using this set of tools, yet I can't find one fully-working example of using the built-in membership, roles, personalization, etc. tools with MySQL *anywhere*!
Has anyone got a set of files with instructions that I can use?
Whew!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm using vs2008 and the rest set of tools just like yours... and i managed to get them working properly but ... only on my local host using local MySQL server....
when i try to connect to my testing website using web hosted MySQL server ... the web site administration tool gives me an error when trying to display the security tab... i tried various connection string combinations but still.. no luck.
if someone has a suggestion i would be more then grateful ...
P.S i used the "Simple.Providers" that I've found here on "the code project" in some other post if I'm not mistaken .
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
My provider supports mysql only with odbc: what could I do in this case? I tried to change Mysql objects in Odbc objects, but I can't login... is there a workaround? Please help... Thank you
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Hi all... I am using this table and control,
1. If the user is giving invalid password it will give one messge as "Your login attempt was not successful. Please try again."
2. If the attempt for more than 5 times with invalid password the user will be locked... at this time also gives same message as "Your login attempt was not successful. Please try again."
I need different message to be displayed let me know how to do this,,, Regards BalaJi
modified on Monday, January 28, 2008 2:27:42 AM
|
| Sign In·View Thread·PermaLink | 1.20/5 (2 votes) |
|
|
|
 |
|
|
Hi,
I have used the work created by Andri (thanks Andri ). But when I uploaded the files to the server I got the following error:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Could not load type 'Andri.Web.MySqlRoleProvider'.
Source Error:
Line 92: Line 93: name="MySqlRoleProvider" Line 94: type="Andri.Web.MySqlRoleProvider" Line 95: connectionStringName="ConnString" Line 96: applicationName="Skillconnect"
Source File: C:\Domains\techniel.com\wwwroot\web.config Line: 94
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
Any suggestions on how I can resolve it?
Thanks, -Haniel
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Hi I had this same error (Parser Error Message: Could not load type 'Andri.Web.MySqlRoleProvider') and after 2.5 hrs searching the net I still had no answer. To make matters worse I had a test project I created a year or so ago using this same code and it worked fine but there seemed to be no differance in the code.
Then I read the instructions again where it says (3. Upload the two files to ~/App_Code)
I realised that what I had done was right clicked on the project and selected 'Add Exsisting Item'.
After clicking Yes to a message asking if I would like to place these classes in a folder called App_Code, VS2005 automaticaly created this folder and placed the classes into it.
Then I got the error.
Now I know this sounds stupid but.... I decided to delete App_Code folder and recreate it manualy. I then right clicked on this new App_Code folder and selected 'Add Exsisting Item'. Selected the 2 classes Ran the project and it worked great!!!
I hope this helps some of you guys.
All the best Hawkmoth
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
On our server we donot have SQLserver or SQLExpress, but when ever I launch the website it is failing to connect to SqlServer. I donot have any connection strings in the web.config file pointing to SQLserver. I am using your role and membership provider files to connect to the MYSQL database.
It looks like whenever we launch the website, it tries to create ASPNETDB.MDF in the app_data directory and since we donot have SQLServer it fails to create.
Any help will be appreciated.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi, Can any one tell me what switches yu to use to compile from the command line please ? I've tried: csc /t:library MySqlRoleProvider.cs /reference:MySql.Data.dll Which creates the .dll. Is this all I need to do ? I hoping this will all work with the VWD Express 2005 tool Many thanks in advance and a good christmas to all who read this
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
The code provided up to, whilst mostly correct, didn't work for me first time. I had to edit the code a little. To save other people the trouble, I've placed the working SQL code (exported from my local database) for others to use:
CREATE TABLE `roles` ( `Rolename` varchar(255) collate latin1_general_ci NOT NULL, `ApplicationName` varchar(255) collate latin1_general_ci NOT NULL, KEY `Rolename` (`Rolename`,`ApplicationName`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE `users` ( `PKID` varchar(36) collate latin1_general_ci NOT NULL, `Username` varchar(255) collate latin1_general_ci NOT NULL, `ApplicationName` varchar(100) collate latin1_general_ci NOT NULL, `Email` varchar(100) collate latin1_general_ci NOT NULL, `Comment` varchar(255) collate latin1_general_ci default NULL, `Password` varchar(128) collate latin1_general_ci NOT NULL, `PasswordQuestion` varchar(255) collate latin1_general_ci default NULL, `PasswordAnswer` varchar(255) collate latin1_general_ci default NULL, `IsApproved` tinyint(1) default NULL, `LastActivityDate` datetime default NULL, `LastLoginDate` datetime default NULL, `LastPasswordChangedDate` datetime default NULL, `CreationDate` datetime default NULL, `IsOnLine` tinyint(1) default NULL, `IsLockedOut` tinyint(1) default NULL, `LastLockedOutDate` datetime default NULL, `FailedPasswordAttemptCount` int(11) default NULL, `FailedPasswordAttemptWindowStart` datetime default NULL, `FailedPasswordAnswerAttemptCount` int(11) default NULL, `FailedPasswordAnswerAttemptWindowStart` datetime default NULL, PRIMARY KEY (`PKID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
CREATE TABLE `usersinroles` ( `Username` varchar(255) collate latin1_general_ci NOT NULL, `Rolename` varchar(255) collate latin1_general_ci NOT NULL, `ApplicationName` varchar(255) collate latin1_general_ci NOT NULL, KEY `Username` (`Username`,`Rolename`,`ApplicationName`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
That will work with MySQL 5 in PhpMyAdmin. Happy ASP.NETing
Thanks -Bursh.
|
| Sign In·View Thread·PermaLink | 3.20/5 (4 votes) |
|
|
|
 |
|
|
 |
|
| |