Partager via


How to Write Site Code to Register Users

You register users in your site code by using the User Profile Manager (UPM) Membership provider. You must make sure that you register the UPM membership provider with ASP.NET in the membership section of the Web.config file, as follows:

<membership defaultProvider="UpmProvider">
<providers>
<add name="UpmProvider" type="Microsoft.CommerceServer.Runtime.Profiles.UpmMembershipProvider" enablePasswordReset="true"/>
</providers>
</membership>

After you register the UPM Membership provider with ASP.NET, you call the CreateUser method of the Membership class.

To create a user by using the UPM Membership Provider

  1. Declare a new function to create a user. For example, you can call the function CreateMembershipUser.

    The function should accept parameters for userName, password, eMail, passwordQuestion, and passwordAnswer.

  2. Create a variable that will hold a value to indicate whether the new user needs approval, or already has approval when the user profile is created. The example in this topic assumes that the new user does not need approval. Therefore, the example creates a variable that is called isApproved and sets its value to true.

  3. Call the CreateUser method of the Membership class, passing it the variable values that were passed into the CreateMembershipUser function. The last parameter, status, is an output parameter that will contain the results of the call.

  4. Return the status to the calling code.

Example

The following example creates a function that is called CreateMembershipUser that creates a user based on parameters that are passed into the function.

private MembershipCreateStatus CreateMembershipUser(
        string userName,
        string password,
        string eMail,
        string passwordQuestion,
        string passwordAnswer
    )
{
    try
    {
        // Declare variables.
        bool isApproved = true;
        MembershipCreateStatus status;

        // Create the user.
        Membership.CreateUser(userName, password, eMail, passwordQuestion, passwordAnswer, isApproved, out status);

        // Return the status.
        return status;
    }
    catch (CommerceException ex)
    {
        Console.WriteLine("Exception: {0}\r\nMessage: {1}", ex.GetType(), ex.Message.ToString());

        // Return generic error code.
        return MembershipCreateStatus.ProviderError;
    }
}

The following code is an example of how to call the CreateMembershipUser procedure:

CreateMembershipUser(
    "someone",
    "myP@ssword",
    "someone@microsoft.com",
    "myQuestion",
    "myAnswer"
);

Compiling the Code

To compile the code, you must include the following namespace directive:

using System.Web.Security;

See Also

Other Resources

How to Create an Instance of a Profile

How to Access an Instance of a Profile

Profiles Concepts and Tasks