Using Kellner's ASP.NET Membership Console with MySql

by Marvin 25. January 2009 08:35

At this point, I've done a few ASP.NET websites, hosted on GoDaddy, that have user accounts using the ASP.NET Membership and Role providers and all the great web controls that ship with ASP.NET.  Each time, I ended up wanting to give the admin users of the site an interface to be able to manage the site membership accounts, grant access privileges, create, edit and remove accounts.  ASP.NET doesn't come with that, but there are some open-source projects out there which can give you this.  I really like Peter Kellner's membership management console which you can download here.  However, when I tried to use it with MySql and the .NET Connector v5.2.3 and deployed to GoDaddy, I ran into some unique challenges that I hadn't encountered when using Sql Server in the past.  This article explains how I worked around the challenges and successfully implemented the tool in a web application using VS Pro 2008, .NET 3.5, MySql 5.0 and the .NET Connector v5.2.3. 

A couple of things before I start:

  1. I will assume that you've already got ASP.NET Membership and Roles successfully implemented in your application.  If not, and you need a walkthrough, I've also got an article on Implementing ASP.NET Membership and Roles using MySql and .NET Connector v5.2.3 that you might want to check out.  I will assume that you've read it or know it and won't repeat any of that information in this article.
  2. If you use some other hosting provider than GoDaddy, particularly one that doesn't use a medium trust policy, or if you are using a newer version of the .NET Connector that v5.2.3, I think it would be worth trying to implement Peter Kellner's suite as is out of the box before doing what I did.  I really think the problems I ran into are related to GoDaddy's medium trust policy in combination with the way the .NET Connector v5.2.3 handles certain methods of the Roles class in the System.Web.Security namespace. 
  3. I made some minor modifications to Kellner's code, which I will describe, and merged two of his examples into one to create the user interface I was going for.  I also threw in a class called DALQuery that I wrote for accessing my data.  You can download the complete code with my modifications here.  
  4. I'm far too new to programming in .NET to assert that I know the right way to do anything.  I'm only sharing what worked for me.  

Ok, from the start, I created a new website (Shift-Alt-N in VS 2008).  I did all the steps in Implementing ASP.NET Membership and Roles using MySql and .NET Connector v5.2.3.  I went to Peter Kellner's website and downloaded this file.  I unzipped it, and copied the contents of his App_Code folder to mine.  Then, I took his Membership.aspx and DetailsViewSample.aspx pages with code behind and sort of merged the two to get the interface I was going for.  I added AJAX update panels because it's just too easy in .NET 3.5. 

At this point, you might be able to just publish what you've got (see point #2 above).  I had problems, though.  To fix them, first of all, I had to modify my web.config.  If you followed the steps in Implementing ASP.NET Membership and Roles using MySql and .NET Connector v5.2.3, then your web.config has two lines that look like this:

        <roleManager enabled="true" defaultProvider="MySQLRoleProvider" />
        <membership defaultProvider="MySQLMembershipProvider" />

I can't tell you why these have to be changed.  I just know that I had to change them to get it working.  Here's what I changed them to:

        <membership defaultProvider="MySQLMembershipProvider">
            <providers>
                <clear/>
                <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=5.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="currentCS" applicationName="/" passwordFormat="hashed" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" requiresQuestionAndAnswer="false"/>
            </providers>
        </membership>
        <roleManager enabled="true" defaultProvider="MySQLRoleProvider">
            <providers>
                <clear/>
                <add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=5.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="currentCS" applicationName="/"/>
            </providers>
        </roleManager>

Keep in mind that "currentCS" is just my own personal convention for naming connection strings.  You will need to put whatever you used for the name of yours.  Of course, there are some attributes in there (like minRequiredPasswordLength) that I added because I wanted to.  They probably have nothing to do with why this fix worked for me.  All I know is that something in the change fixed the problem.

From that point, things worked ok except when I tried to add users to a role or remove them from one using the Membership.aspx page.  On GoDaddy, I would get a runtime security error whenever either of these lines of code tried to execute: 

        Roles.RemoveUsersFromRole(userNameArray, roleName);

or

        Roles.AddUserToRole(userName, roleName);

Again, I really think the .NET Connector is the real source of the trouble, because these lines work just fine with SQL Server on GoDaddy.  They are found in both Membership.aspx.cs and in RoleDataObject.cs.  Since it looked like the problem was triggered by use of these methods in the Role Provider, I decided to not use them and instead mimic what I thought they did with SQL statements on the table my_aspnet_Roles.  (As an aside, I am using a little class I created called DALQuery which takes care of some of the monotonous coding involved in accessing the database.  It's included in the code download for this article.)

Here's one example of this mimicking from the ToggleInRole_Click handler in Membership.aspx.cs.  I changed this peice of code:

        if (whatToDo.StartsWith("Rem"))
        {
            Roles.RemoveUsersFromRole(userNameArray, roleName);
        }
        else
        {
            Roles.AddUserToRole(userName, roleName);
        }

to this:

        // get the user id from the database for the user name
        string strSql = "SELECT `id` FROM `my_aspnet_Users` WHERE `name` = '" + userName + "';";
        DALquery qryForUserId = new DALquery();
        string userId = qryForUserId.getString(strSql);

        // get the role id from the database for the role name
        strSql = "SELECT `id` FROM `my_aspnet_Roles` WHERE `name` = '" + roleName + "';";
        DALquery qryForRoleId = new DALquery();
        string roleId = qryForRoleId.getString(strSql);

        // either add or remove the user from the role
        if (whatToDo.StartsWith("Rem"))
        {
            strSql = "DELETE FROM `my_aspnet_UsersInRoles` WHERE `userId` = '" + userId + "' AND `roleId` = '"; + roleId + "';";
            DALquery qryDeleteFromRole = new DALquery();
            qryDeleteFromRole.modifyData(strSql);
        }
        else
        {
            strSql = "INSERT INTO `my_aspnet_UsersInRoles` (`userId`, `roleId`) VALUES ('" + userId + "', '" + roleId + "');";
            DALquery qryAddToRole = new DALquery();
            qryAddToRole.modifyData(strSql);
        }

That's basically it. After making these changes and publishing to GoDaddy, I had a nice membership management console, courtesy of Peter Kellner, that would allow the admins on the site to manage their users.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

Web Development

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author

I am a junior-level C# .NET developer living in Nashville, TN.  I'm currently working in biomedical informatics, developing a web service, a MySql database, and a web application.  Every week or so, I spend hours trying to figure out how to do something, and after I find the solution, I really want to make sure I don't go through that exercise again.  I love to write.  It helps me to remember things.  So, I use this blog as a way to document those painful lessons as I learn them.  It has already helped me to be able to refer back to them.  I hope some of these will save someone else some time as well.