Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nigel Wilson 945 posts 2077 karma points
    Dec 10, 2010 @ 03:38
    Nigel Wilson
    0

    Membership Provider - Alert User To Existing Username

    I have been working through Mike Taylors blog postings on creating / authenticating new members before activating them. 

    I have the users being created OK, but I have been doing some testing  for duplicate usernames.

    In the code behind there is the following:

     

    protected void cwMember_CreatedUser(object sender, EventArgs e)
    {
    CreateUserWizard cuw = (CreateUserWizard)sender;
    MembershipUser user = Membership.GetUser(cuw.UserName);
    if (user != null)
    {
    ...
    }

    I thought I could add an else condition to this if statement for when the username already existed and display an error message on screen - below the username field within the user control.

    So far this hasn't worked.

    Because I don't fully understand .net Membership can anyone advise if this is totally off-course and there is a better way to handle it ?

    Thanks

    Nigel

     

  • Thomas Stock 40 posts 70 karma points
    Dec 10, 2010 @ 09:29
    Thomas Stock
    1

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard.oncreateduser.aspx.
    "Raises the CreatedUser event after the membership provider creates the user account."

    > When you enter the CreatedUser method, the user has already been created, so user will never be null.

    Handle the OnCreateUserError event instead:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createuserwizard.oncreateusererror.aspx

    This method returns a CreateUserErrorEventArgs object that holds the reason why user creation failed:
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.createusererroreventargs.aspx

    protected void OnCreateUserError(object sender, System.Web.UI.WebControls.CreateUserErrorEventArgs e)
    {

    switch (e.CreateUserError)
    {
    case MembershipCreateStatus.DuplicateUserName:
    Label1.Text = "Username already exists. Please enter a different user name.";
    break;


    case MembershipCreateStatus.DuplicateEmail:
    Label1.Text = "A username for that e-mail address already exists. Please enter a different e-mail address.";
    break;

    case MembershipCreateStatus.InvalidPassword:
    Label1.Text = "The password provided is invalid. Please enter a valid password value.";
    break;

    ...

     

  • Nigel Wilson 945 posts 2077 karma points
    Dec 10, 2010 @ 18:26
    Nigel Wilson
    0

    Hi Thomas

    Wicked - thank you so much for your response.

    My post exposes my limited .net knowledge but it is a wicked feeling when you get something to work - albeit with excellent help from others. Sooo much to learn, sooo little time!

    For others who may find this post the other thing to do was addthe OnCreateUserError="whatever you called the method" part to the CreateUserWizard tag as per below.

    <asp:CreateUserWizard ID="cwMember" runat="server" LoginCreatedUser="False" DisableCreatedUser="True"
                        OnCreatedUser="cwMember_CreatedUser" OnCreateUserError="cwMember_OnCreateUserError"
                        CreateUserButtonText="Join PwP" CreateUserButtonStyle-CssClass="btnJoin">

    Thanks again Thomas - an awsome start to my day.

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft