Using Membeship ObjectDataSource For Returning ProviderUserKey (ForeignKey Use)

I was recently asked how to use the Membership Data Object to return the unique ProviderKey generated by the Membership object. This may be necessary if want to associate the membership data with an existing table. That is, add a foreign key "ProviderUserKey" to another table so you can track an existing user to your current database without getting involved in extending the base provider. You can of course extend the existing provider to include other fields, but in general, you probably are better off maintaining your own tables rather than extending the provider model. Peter Dawson does an excellent job of explaining and showing how to extend the Membership Provider in his article Creating a Custom ASP.NET Provider. (published on theserverside.net)

So, back to what needs to be done to use my Membership ObjectDataClass (Part 2: Implementation" href="http://peterkellner.net/archives/2006/01/09/24/" rel=bookmark>Microsoft ASP.NET 2.0 Member/Role Management with IIS

Part 2: Implementation

). First thing is you need to add another insert method to the class. This method takes just a few parameters and returns as out parameters both the status of the insert as well as the providerUserKey as a string.

[DataObjectMethod(DataObjectMethodType.Insert, false)]
static public void Insert(string userName,
    bool isApproved,
    string email,
    string passwordQuestion,
    string password,
    string passwordAnswer,
    out MembershipCreateStatus membershipCreateStatus,
    out string providerIdKeyString)
{
    MembershipUser muNew = Membership.CreateUser(userName,
        password, email, passwordQuestion, passwordAnswer,
        isApproved,out membershipCreateStatus);
    if (membershipCreateStatus ==
        MembershipCreateStatus.Success)
    {
        providerIdKeyString = muNew.ProviderUserKey.ToString();
    }
    else
    {
        providerIdKeyString = string.Empty;  // failed
    }
    return;
}

Figure 1 – The New Insert Method to be added to MembershipUserODS.cs

If you want to turn the Guid passed back into a native Guid type do the following. (this could be useful if you want to retrieve the current username as shown below.

Guid myNewGuild = new Guid(providerIdKeyString);
MembershipUser myCurrentMembershipUser = Membership.GetUser(myNewGuild);
string memberName = myCurrentMembershipUser.UserName;

Figure 2 – Example of converting created ProviderUseKey to Guid and retreiving username

Now that we have the new Insert Method defined the easiest thing to do is use it directly. Below is a complete aspx page that references the new method as well as a screen shot of what it looks like to add a new member. Notice that the status label is returning the providerUserKey.

Figure 3 – Examples of adding a new user with a valid password, and one without a valid password

< %@ Page Language="C#" %>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    protected void ButtonNewUser_Click(object sender, EventArgs e)
    {
        MembershipCreateStatus membershipCreateStatus;
        string providerIdKeyString = string.Empty;
        MembershipUtilities.MembershipUserODS.Insert(TextBoxUserName.Text,
            CheckboxApproval.Checked,
            TextBoxEmail.Text,
            TextBoxPasswordQuestion.Text,
            TextBoxPassword.Text,
            TextBoxPasswordAnswer.Text,
            out membershipCreateStatus,
            out providerIdKeyString
        );
        if (membershipCreateStatus == MembershipCreateStatus.Success)
        {
            LabelInsertMessage.Text = "New User Create Successfully.  Key: " + providerIdKeyString;
        }
        else
        {
            LabelInsertMessage.Text = membershipCreateStatus.ToString();
        }
 
        return;
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp 😛 anel ID="PanelCreateUser" runat="server" BorderColor="Black" BorderWidth="1px"
                Height="50px" Width="125px">
                <table cellpadding="3" cellspacing="3">
                    <tr>
                        <td style="height: 32px">
                            <asp :Label ID="Label3" runat="server" Text="UserName"></asp>
                        </td>
                        <td style="height: 32px">
                            <asp :TextBox ID="TextBoxUserName" runat="server"></asp>
                        </td>
                        <td style="height: 32px">
                            <asp :Label ID="Label4" runat="server" Text="Password"></asp>
                        </td>
                        <td style="height: 32px">
                            <asp :TextBox ID="TextBoxPassword" runat="server"></asp>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp :Label ID="Label5" runat="server" Text="PasswordQuestion"></asp>
                        </td>
                        <td>
                            <asp :TextBox ID="TextBoxPasswordQuestion" runat="server"></asp>
                        </td>
                        <td>
                            <asp :Label ID="Label6" runat="server" Text="PasswordAnswer"></asp>
                        </td>
                        <td>
                            <asp :TextBox ID="TextBoxPasswordAnswer" runat="server"></asp>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp :Label ID="Label2" runat="server" Text="Email"></asp>
                        </td>
                        <td>
                            <asp :TextBox ID="TextBoxEmail" runat="server"></asp>
                        </td>
                        <td>
                            <asp :Label ID="Label9" runat="server" Text="Approved"></asp>
                        </td>
                        <td>
                            <asp :CheckBox ID="CheckboxApproval" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp :Button ID="ButtonNewUser" runat="server" OnClick="ButtonNewUser_Click" Text="Create New User" />
                        </td>
                    </tr>
                </table>
                </asp><asp :Label ID="LabelInsertMessage" runat="server"></asp>
        </div>
    </form>
</body>
</html>

Figure 4 – complete aspx page excercising new insert method

About Peter Kellner

Follow me:


Comments

  1. Thanks Andy, Happy to get any corrections. I’ve updated the article.
    -Peter

  2. Administrator says:

    I prefer to use ProviderUserKey because it is a guid for my foreign key references.
    -Peter Kellner

  3. I read in a forum response that it is not recommended to use UserID as a foreign key in your own tables. The reason given was that this was a provider dependent key that should be transparent to developers. I believe the recommendation was to use the UserName field as the key instead. This is provider independent. Any thoughts on this?

  4. George Pearce says:

    Your code works but my development is in vb.net. Any chance of getting the source in vb.net? tks.

Trackbacks

  1. Microsoft ASP.NET 2.0 Member/Role Management with IIS

Follow

Get every new post delivered to your Inbox

Join other followers: