Error on signup if e-mail exists

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Error on signup if e-mail exists

Post by Brewhaus » Fri Apr 03, 2009 7:52 am

We have had a couple of customers who have tried to create a new account, but their e-mail address is already in the system. Because of the adaptation of our sign in page, we are getting a runtime error instead of simply telling the customer that the e-mail address already exists. Here is our RegisterDialog.ascx file

Code: Select all

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RegisterDialog.ascx.cs" Inherits="ConLib_RegisterDialog" %>
<%--
<conlib>
<summary>Display a form using that a new customer can register for a new account.</summary>
</conlib>
--%>
<div class="dialogSection">
    <div class="header">
        <h2><asp:Localize ID="Caption" runat="server" Text="New Customers"></asp:Localize></h2>
    </div>
    <div class="content nofooter">
        <table class="inputForm" cellpadding="0" cellspacing="0" align="center">
            <tr>
                <td colspan="3">
                    <asp:Label ID="InstructionText" runat="server" EnableViewState="False" Text="If you are a new customer, take a moment to register below."></asp:Label>
                    <asp:ValidationSummary ID="RegisterValidationSummary" runat="server" ValidationGroup="Register" />
                </td>
            </tr>
            <tr>
                <td align="center" colspan="3">
                    <asp:Label ID="FailureText" runat="server" EnableViewState="False" SkinID="ErrorCondition"></asp:Label>
                </td>
            </tr>
            <tr>
                <th class="rowHeader">
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" Text="Email:"></asp:Label>
                </th>
                <td align="left" colspan="2">
                    <asp:TextBox ID="UserName" runat="server" Columns="30"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                        ErrorMessage="Email address is required" ToolTip="Email address is required" 
                        ValidationGroup="Register" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="EmailValidator" runat="server" ControlToValidate="UserName"
                        ErrorMessage="Email address is not valid." ToolTip="Email address is not valid." 
                        ValidationGroup="Register" Display="Dynamic" Text="*"></asp:RegularExpressionValidator>
                    <asp:CustomValidator ID="InvalidRegistration" runat="server" ControlToValidate="UserName"
                        ErrorMessage="Registration was not successful." ToolTip="Registration was not successful."
                        Display="Dynamic" ValidationGroup="Register" Text="*"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align="left" colspan="2">
                    <asp:CheckBox ID="RememberUserName" runat="server" Text="Remember Me" />
                </td>
            </tr>
            <tr>
                <th class="rowHeader">
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Text="Password:"></asp:Label>
                </th>
                <td align="left">
                    <asp:TextBox ID="Password" runat="server" TextMode="Password" Width="80" MaxLength="24"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                        ErrorMessage="You must provide a password" ToolTip="You must provide a password"
                        ValidationGroup="Register" Text="*"></asp:RequiredFieldValidator>
                    <asp:PlaceHolder ID="PasswordValidatorPanel" runat="server" EnableViewState="false"></asp:PlaceHolder>
                </td>
                <td rowspan="2" valign="top">
                    <div style="width:220px;text-align:justify">
                    <i><asp:Localize ID="PasswordPolicyLength" runat="server" Text="Your password must be at least {0} characters long."></asp:Localize>
                    <asp:Localize ID="PasswordPolicyRequired" runat="server" Text="You must include at least one {0}."></asp:Localize></i>
                    </div>
                </td>
            </tr>
            <tr>
                <th class="rowHeader">
                    <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword" Text="Re-enter:"></asp:Label>
                </th>
                <td align="left">
                    <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password" Width="80" MaxLength="24"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
                        ErrorMessage="You must re-enter the password." ToolTip="You must re-enter the password."
                        ValidationGroup="Register" Text="*"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                        ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="You did not re-enter the password correctly."
                        ValidationGroup="Register" Text="*"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td colspan="2">
                    <asp:Button ID="RegisterButton" runat="server" Text="Register" OnClick="RegisterButton_Click" ValidationGroup="Register" />
                </td>
            </tr>
        </table>
    </div>
</div>
Can anyone see where the problem is?
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Error on signup if e-mail exists

Post by mazhar » Fri Apr 03, 2009 8:06 am

Unable to reproduce the case on 7.0.2. Post .cs file as well in thread, it will be something wrong up there.

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: Error on signup if e-mail exists

Post by Brewhaus » Fri Apr 03, 2009 9:21 am

We are currently running 7.0. I assume, though, that it is the code, and not the version. The .cs file is below.

Code: Select all

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommerceBuilder.Common;
using CommerceBuilder.Users;
using CommerceBuilder.Utility;

public partial class ConLib_RegisterDialog : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //SET VALIDATION EXPRESSION
        EmailValidator.ValidationExpression = ValidationHelper.EmailRegex;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        PageHelper.ConvertEnterToTab(UserName);
        PageHelper.ConvertEnterToTab(Password);
        PageHelper.SetDefaultButton(ConfirmPassword, RegisterButton.ClientID);
        ShowPasswordPolicy();
    }

    protected void RegisterButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && ValidatePassword())
        {
            MembershipCreateStatus status;
            User newUser = UserDataSource.CreateUser(UserName.Text, Password.Text, string.Empty, string.Empty, true, 0, out status);
UserGroup userGroup = new UserGroup(newUser.UserId, 11);
            newUser.UserGroups.Add(userGroup);
            newUser.IsApproved = true;
            newUser.Save();
            //Some custom message code will be here
//return;
            if (status == MembershipCreateStatus.Success)
            {
                if (Membership.ValidateUser(UserName.Text, Password.Text))
                {
                    if (RememberUserName.Checked)
                    {
                        HttpCookie cookie = new HttpCookie("UserName", UserName.Text);
                        cookie.Expires = DateTime.MaxValue;
                        Response.Cookies.Add(cookie);
                        Trace.Write("Added UserName Cookie to Response");
                    }
                    else
                    {
                        Response.Cookies.Add(new HttpCookie("UserName", ""));
                    }
                    //MIGRATE USER IF NEEDED
                    int newUserId = UserDataSource.GetUserId(UserName.Text);
                    if ((Token.Instance.UserId != newUserId) && (newUserId != 0))
                    {
                        User.Migrate(Token.Instance.User, newUser);
                        Token.Instance.UserId = newUserId;
                    }
                    //REDIRECT TO APPROPRIATE PAGE
                    FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
                }
            }
            else
            {
                InvalidRegistration.IsValid = false;
                switch (status)
                {
                    case MembershipCreateStatus.DuplicateUserName:
                    case MembershipCreateStatus.DuplicateEmail:
                        InvalidRegistration.ErrorMessage = "That email address is already registered.  Sign in to access your account.";
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                    case MembershipCreateStatus.InvalidUserName:
                        InvalidRegistration.ErrorMessage = "The email address you provided is not valid.";
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        InvalidRegistration.ErrorMessage = "The password you provided is not valid.";
                        break;
                    default:
                        InvalidRegistration.ErrorMessage = "Unexpected error in registration (" + status.ToString() + ")";
                        break;
                }
            }
        }
    }

    private bool _PasswordLengthValidatorAdded = false;
    private void ShowPasswordPolicy()
    {
        //SHOW THE PASSWORD POLICY
        CustomerPasswordPolicy policy = new CustomerPasswordPolicy();
        if (policy.MinLength > 0)
        {
            PasswordPolicyLength.Text = string.Format(PasswordPolicyLength.Text, policy.MinLength);
            if (!_PasswordLengthValidatorAdded)
            {
                RegularExpressionValidator PasswordLengthValidator = new RegularExpressionValidator();
                PasswordLengthValidator.ID = "PasswordLengthValidator";
                PasswordLengthValidator.EnableViewState = false;
                PasswordLengthValidator.ControlToValidate = "Password";
                PasswordLengthValidator.Text = "*";
                PasswordLengthValidator.ErrorMessage = "Password must be at least " + policy.MinLength.ToString() + " characters.";
                PasswordLengthValidator.ValidationExpression = ".{" + policy.MinLength.ToString() + ",}";
                PasswordLengthValidator.SetFocusOnError = false;
                PasswordLengthValidator.EnableClientScript = false;
                PasswordLengthValidator.ValidationGroup = "Register";
                PasswordValidatorPanel.Controls.Add(PasswordLengthValidator);
                _PasswordLengthValidatorAdded = true;
            }
        }
        else PasswordPolicyLength.Visible = false;
        List<string> requirements = new List<string>();
        if (policy.RequireUpper) requirements.Add("uppercase letter");
        if (policy.RequireLower) requirements.Add("lowercase letter");
        if (policy.RequireNumber) requirements.Add("number");
        if (policy.RequireSymbol) requirements.Add("symbol");
        if (!policy.RequireNumber && !policy.RequireSymbol && policy.RequireNonAlpha) requirements.Add("non-letter");
        PasswordPolicyRequired.Visible = (requirements.Count > 0);
        if (PasswordPolicyRequired.Visible)
        {
            if (requirements.Count > 1) requirements[requirements.Count - 1] = "and " + requirements[requirements.Count - 1];
            PasswordPolicyRequired.Text = string.Format(PasswordPolicyRequired.Text, string.Join(", ", requirements.ToArray()));
        }
    }

    private bool ValidatePassword()
    {
        //VALIDATE PASSWORD POLICY
        CustomerPasswordPolicy policy = new CustomerPasswordPolicy();
        if (!policy.TestPassword(null, Password.Text))
        {
            CustomValidator policyValidator = new CustomValidator();
            policyValidator.ControlToValidate = "Password";
            policyValidator.IsValid = false;
            policyValidator.Text = "*";
            policyValidator.ErrorMessage = "The password does not meet the minimum requirements.";
            policyValidator.SetFocusOnError = false;
            policyValidator.ValidationGroup = "Register";
            PasswordValidatorPanel.Controls.Add(policyValidator);
            return false;
        }
        return true;
    }
}
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Error on signup if e-mail exists

Post by mazhar » Fri Apr 03, 2009 10:02 am

You got a customization in control and it seems that problem is with custom part.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Error on signup if e-mail exists

Post by mazhar » Fri Apr 03, 2009 10:04 am

Try following code in CS file, I think it will fix the problem. In fact old code is not checking that user is already member of user group or not and forces to add him again.

Code: Select all

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommerceBuilder.Common;
using CommerceBuilder.Users;
using CommerceBuilder.Utility;

public partial class ConLib_RegisterDialog : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //SET VALIDATION EXPRESSION
        EmailValidator.ValidationExpression = ValidationHelper.EmailRegex;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        PageHelper.ConvertEnterToTab(UserName);
        PageHelper.ConvertEnterToTab(Password);
        PageHelper.SetDefaultButton(ConfirmPassword, RegisterButton.ClientID);
        ShowPasswordPolicy();
    }

    protected void RegisterButton_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && ValidatePassword())
        {
            MembershipCreateStatus status;
            User newUser = UserDataSource.CreateUser(UserName.Text, Password.Text, string.Empty, string.Empty, true, 0, out status);
            if (newUser != null)
            {
                if (!newUser.IsInGroup(11))
                {
                    UserGroup userGroup = new UserGroup(newUser.UserId, 11);
                    newUser.UserGroups.Add(userGroup);
                    newUser.IsApproved = true;
                    newUser.Save();
                }
            }

            //Some custom message code will be here
            //return;
            if (status == MembershipCreateStatus.Success)
            {
                if (Membership.ValidateUser(UserName.Text, Password.Text))
                {
                    if (RememberUserName.Checked)
                    {
                        HttpCookie cookie = new HttpCookie("UserName", UserName.Text);
                        cookie.Expires = DateTime.MaxValue;
                        Response.Cookies.Add(cookie);
                        Trace.Write("Added UserName Cookie to Response");
                    }
                    else
                    {
                        Response.Cookies.Add(new HttpCookie("UserName", ""));
                    }
                    //MIGRATE USER IF NEEDED
                    int newUserId = UserDataSource.GetUserId(UserName.Text);
                    if ((Token.Instance.UserId != newUserId) && (newUserId != 0))
                    {
                        User.Migrate(Token.Instance.User, newUser);
                        Token.Instance.UserId = newUserId;
                    }
                    //REDIRECT TO APPROPRIATE PAGE
                    FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
                }
            }
            else
            {
                InvalidRegistration.IsValid = false;
                switch (status)
                {
                    case MembershipCreateStatus.DuplicateUserName:
                    case MembershipCreateStatus.DuplicateEmail:
                        InvalidRegistration.ErrorMessage = "That email address is already registered.  Sign in to access your account.";
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                    case MembershipCreateStatus.InvalidUserName:
                        InvalidRegistration.ErrorMessage = "The email address you provided is not valid.";
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        InvalidRegistration.ErrorMessage = "The password you provided is not valid.";
                        break;
                    default:
                        InvalidRegistration.ErrorMessage = "Unexpected error in registration (" + status.ToString() + ")";
                        break;
                }
            }
        }
    }

    private bool _PasswordLengthValidatorAdded = false;
    private void ShowPasswordPolicy()
    {
        //SHOW THE PASSWORD POLICY
        CustomerPasswordPolicy policy = new CustomerPasswordPolicy();
        if (policy.MinLength > 0)
        {
            PasswordPolicyLength.Text = string.Format(PasswordPolicyLength.Text, policy.MinLength);
            if (!_PasswordLengthValidatorAdded)
            {
                RegularExpressionValidator PasswordLengthValidator = new RegularExpressionValidator();
                PasswordLengthValidator.ID = "PasswordLengthValidator";
                PasswordLengthValidator.EnableViewState = false;
                PasswordLengthValidator.ControlToValidate = "Password";
                PasswordLengthValidator.Text = "*";
                PasswordLengthValidator.ErrorMessage = "Password must be at least " + policy.MinLength.ToString() + " characters.";
                PasswordLengthValidator.ValidationExpression = ".{" + policy.MinLength.ToString() + ",}";
                PasswordLengthValidator.SetFocusOnError = false;
                PasswordLengthValidator.EnableClientScript = false;
                PasswordLengthValidator.ValidationGroup = "Register";
                PasswordValidatorPanel.Controls.Add(PasswordLengthValidator);
                _PasswordLengthValidatorAdded = true;
            }
        }
        else PasswordPolicyLength.Visible = false;
        List<string> requirements = new List<string>();
        if (policy.RequireUpper) requirements.Add("uppercase letter");
        if (policy.RequireLower) requirements.Add("lowercase letter");
        if (policy.RequireNumber) requirements.Add("number");
        if (policy.RequireSymbol) requirements.Add("symbol");
        if (!policy.RequireNumber && !policy.RequireSymbol && policy.RequireNonAlpha) requirements.Add("non-letter");
        PasswordPolicyRequired.Visible = (requirements.Count > 0);
        if (PasswordPolicyRequired.Visible)
        {
            if (requirements.Count > 1) requirements[requirements.Count - 1] = "and " + requirements[requirements.Count - 1];
            PasswordPolicyRequired.Text = string.Format(PasswordPolicyRequired.Text, string.Join(", ", requirements.ToArray()));
        }
    }

    private bool ValidatePassword()
    {
        //VALIDATE PASSWORD POLICY
        CustomerPasswordPolicy policy = new CustomerPasswordPolicy();
        if (!policy.TestPassword(null, Password.Text))
        {
            CustomValidator policyValidator = new CustomValidator();
            policyValidator.ControlToValidate = "Password";
            policyValidator.IsValid = false;
            policyValidator.Text = "*";
            policyValidator.ErrorMessage = "The password does not meet the minimum requirements.";
            policyValidator.SetFocusOnError = false;
            policyValidator.ValidationGroup = "Register";
            PasswordValidatorPanel.Controls.Add(policyValidator);
            return false;
        }
        return true;
    }
}

Brewhaus
Vice Admiral (VADM)
Vice Admiral (VADM)
Posts: 878
Joined: Sat Jan 19, 2008 4:30 pm

Re: Error on signup if e-mail exists

Post by Brewhaus » Fri Apr 03, 2009 10:58 am

That appears to work perfectly. Thank you!
Rick Morris
Brewhaus (America) Inc.
Hot Sauce Depot

Post Reply