I was tasked with member creation on a site that was already developed for Active Directory, so the log in functionality was there and most of the site was all ready to go (after minor major tweaks).Here is what I came up with for my create user method (Button_Click Event).Please let me know what you think.protected void createUser_Click(object sender, EventArgs e){    var createStatus = new MembershipCreateStatus();    if (IsBarNumberAndEmailValid(email.Text, barNumber.Text))    {        if (password.Text == confirmPassword.Text)        {            var newUser = Membership.CreateUser(username.Text, password.Text, email.Text, securityQuestion.Text, securityAnswer.Text, false, out createStatus);            switch (createStatus)            {                case MembershipCreateStatus.Success:                    newUser.Comment = barNumber.Text;                    userCreationResults.Text = User account was created successfully. \n Please check your Email for an activation link;                    SendActivationEmail(newUser);                    //Roles.AddUserToRole(newUser.ToString(), Attorney);                    Membership.UpdateUser(newUser);                    break;                case MembershipCreateStatus.DuplicateUserName:                    userCreationResults.Text = That username is already taken, please choose another username.;                    break;                case MembershipCreateStatus.DuplicateEmail:                    userCreationResults.Text = A user with that E-mail address already exists.;                    break;                case MembershipCreateStatus.InvalidEmail:                    userCreationResults.Text = Please Enter a valid E-mail address.;                    break;                case MembershipCreateStatus.InvalidAnswer:                    userCreationResults.Text = The security answer is not valid.;                    break;                case MembershipCreateStatus.InvalidPassword:                    userCreationResults.Text = Please enter a valid Password. please enter a password with at least 7 characters, 1 number and one non-alphanumeric;                    break;                default:                    userCreationResults.Text = Unknown Error: Account NOT created.;                    break;            }        }        else        {            userCreationResults.Text = Confirmation Password must match the Password;        }    }    else    {        userCreationResults.Text = Your Email and/or Bar Number doesn't match UJS Records.;    }}the following are Text BoxespasswordbarNumberemailconfirmPasswordsecurityQuestionsecurityAnsweruserCreationResults is a label.IsBarNumberAndEmailValid is obviously a Boolean method that checks to make sure the bar number and email match a attorney record in a 3rd-party application.
Create a new Member
c#;authorization
You shouldn't place this inside the button click eventhandler, but to a separate method. Assume you want to create a user at some other place, what will you do? Refactoring so the code in question is placed in a separate method or misusing the eventhandler ?  First I would add a method which returns a String based on a MembershipCreateStatus enum value.  private string GetMessage(MembershipCreateStatus status){    switch (createStatus)    {        case MembershipCreateStatus.Success:           return User account was created successfully. \n Please check your Email for an activation link;        case MembershipCreateStatus.DuplicateUserName:            return That username is already taken, please choose another username.;        case MembershipCreateStatus.DuplicateEmail:            return A user with that E-mail address already exists.;        case MembershipCreateStatus.InvalidEmail:            return Please Enter a valid E-mail address.;        case MembershipCreateStatus.InvalidAnswer:            return The security answer is not valid.;        case MembershipCreateStatus.InvalidPassword:           return Please enter a valid Password. please enter a password with at least 7 characters, 1 number and one non-alphanumeric;        default:            return Unknown Error: Account NOT created.;    }}  now let us add a method private string CreateUser(){    if (!IsBarNumberAndEmailValid(email.Text, barNumber.Text))     {         return Your Email and/or Bar Number doesn't match UJS Records.;    }    if (password.Text != confirmPassword.Text)    {        return Confirmation Password must match the Password;    }    var createStatus = new MembershipCreateStatus();    var newUser = Membership.CreateUser(username.Text, password.Text, email.Text, securityQuestion.Text, securityAnswer.Text, false, out createStatus);    if(createStatus == MembershipCreateStatus.Success)    {        newUser.Comment = barNumber.Text;        SendActivationEmail(newUser);        Membership.UpdateUser(newUser);    }    return GetMessage(createStatus);}  which will reduce the button click handler code to  protected void createUser_Click(object sender, EventArgs e){    userCreationResults.Text = CreateUser();}  