Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

CreateUserWizrard Failing with System.NullReferenceException

$
0
0

Hi

I was wondering if someone could help me with an issue I am having.  I have a createuserwizard which I am customizing.  However, I seem to be getting a System.NullReferenceException when clicking the createuser button.  Please see code attached: 

<asp:CreateUserWizard ID="CreateUserWizard" runat="server" ActiveStepIndex="0" 
                    CancelDestinationPageUrl="~/Home.aspx" ><WizardSteps><asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server"><ContentTemplate><asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label><br /><asp:TextBox ID="UserName" CssClass="singleline" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:RequiredFieldValidator><br /><br /><asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label><br /><asp:TextBox ID="Password" runat="server" CssClass="singleline" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:RequiredFieldValidator><br /><br /><asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label><br /><asp:TextBox ID="ConfirmPassword" runat="server" CssClass="singleline" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword" ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:RequiredFieldValidator><br /><br /><asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label><br /><asp:TextBox ID="Email" runat="server" CssClass="singleline" ></asp:TextBox><asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:RequiredFieldValidator><br /><br />    <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label><br /><asp:TextBox ID="Question" runat="server" CssClass="singleline"></asp:TextBox><asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question" ErrorMessage="Security question is required." ToolTip="Security question is required." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:RequiredFieldValidator><br /><br /><asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label><br /><asp:TextBox ID="Answer" runat="server" CssClass="singleline" ></asp:TextBox><asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer" ErrorMessage="Security answer is required." ToolTip="Security answer is required." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:RequiredFieldValidator><br /><br /><asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match." ValidationGroup="CreateUserWizard1" CssClass="validation"></asp:CompareValidator><asp:Label ID="ErrorLabel" runat="server"></asp:Label><asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal></ContentTemplate><CustomNavigationTemplate><asp:Button ID="CancelButton" runat="server" Text="Cancel" CssClass="button" CommandName="Cancel" />        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;        <asp:Button ID="StepNextButton" runat="server" Text="Create User" ValidationGroup="CreateUserWizard1" onclick="CreateUserWizardStep1_Click" CssClass="button" /></CustomNavigationTemplate></asp:CreateUserWizardStep><asp:WizardStep runat="server" Title="Additional Information"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></asp:WizardStep><asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" 
                            AllowReturn="False"></asp:CompleteWizardStep></WizardSteps></asp:CreateUserWizard>

The code behind is as follows:

protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void CreateUserWizardStep1_Click(object sender, EventArgs e)
    {
        if (CreateUserWizard.ActiveStepIndex == 0)
        {
            TextBox UserName = (TextBox)CreateUserWizard.FindControl("UserName");
            TextBox Password = (TextBox)CreateUserWizard.FindControl("Password");
            TextBox ConfirmPassword = (TextBox)CreateUserWizard.FindControl("ConfirmPassword");
            TextBox EmailAddress = (TextBox)CreateUserWizard.FindControl("Email");
            TextBox SecurityQuestion = (TextBox)CreateUserWizard.FindControl("Question");
            TextBox SecurityAnswer = (TextBox)CreateUserWizard.FindControl("Answer");
            Label ErrorLabel = (Label)CreateUserWizard.FindControl("ErrorLabel");

            MembershipCreateStatus status;
            try
            {
                MembershipUser createuser = Membership.CreateUser(UserName.Text, Password.Text, EmailAddress.Text, SecurityQuestion.Text, SecurityAnswer.Text, true, out status);

                if (createuser == null)
                {
                    //UserAccountCreationLabel1.Visible = true;
                    ErrorLabel.Text = GetErrorMessage(status);
                }
                else
                {
                    Roles.AddUserToRole(UserName.Text, "Normal");                   
                }
            }
            catch
            {
                ErrorLabel.Text = "An exception occurred creating the user. Please email system administrator";
            }
        }
        else
        {
            Response.Redirect("Home.aspx");
        }
    }

    public string GetErrorMessage(MembershipCreateStatus status)
    {
        switch (status)
        {
            case MembershipCreateStatus.DuplicateUserName:
                return "Username already exists. Please enter a different user name.";

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

            case MembershipCreateStatus.InvalidPassword:
                return "The password provided is invalid. Please enter a valid password value.";

            case MembershipCreateStatus.InvalidEmail:
                return "The e-mail address provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidAnswer:
                return "The password retrieval answer provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidQuestion:
                return "The password retrieval question provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.InvalidUserName:
                return "The user name provided is invalid. Please check the value and try again.";

            case MembershipCreateStatus.ProviderError:
                return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            case MembershipCreateStatus.UserRejected:
                return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

            default:
                return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
        }
    }

I am putting in data I know that should work.  However, when I run the code in debug mode I can see it is going straight to the catch statement ErrorLabel and producing a System.NullReferenceException.

Can anyone see where my code is going wrong?

Thanks

Rob


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>