Skip to content

Using Membeship ObjectDataSource For Returning ProviderUserKey (ForeignKey Use)

Updated: at 05:05 PM

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="https://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”>

<span class="kwrd">protected</span> <span class="kwrd">void</span> ButtonNewUser_Click(<span class="kwrd">object</span> sender, EventArgs e)
{
    MembershipCreateStatus membershipCreateStatus;
    <span class="kwrd">string</span> providerIdKeyString = <span class="kwrd">string</span>.Empty;
    MembershipUtilities.MembershipUserODS.Insert(TextBoxUserName.Text,
        CheckboxApproval.Checked,
        TextBoxEmail.Text,
        TextBoxPasswordQuestion.Text,
        TextBoxPassword.Text,
        TextBoxPasswordAnswer.Text,
        <span class="kwrd">out</span> membershipCreateStatus,
        <span class="kwrd">out</span> providerIdKeyString
    );
    <span class="kwrd">if</span> (membershipCreateStatus == MembershipCreateStatus.Success)
    {
        LabelInsertMessage.Text = <span class="str">"New User Create Successfully.  Key: "</span> + providerIdKeyString;
    }
    <span class="kwrd">else</span>
    {
        LabelInsertMessage.Text = membershipCreateStatus.ToString();
    }

    <span class="kwrd">return</span>;
}

</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 :P 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