views:

34

answers:

1

In my Web forms applications I had been wiring my Asp.net membership register controls event "OnCreatingUser" to do my checks for whether the user name or email exits or if the user name is appropriate.

What is the equivalent method in Mvc and how is it used?

Here is part of my method from a web forms application.

public void cuwRegister_CreatingUser(object sender, LoginCancelEventArgs e)
{
  TextBox textBox = (TextBox)cuwRegister.CreateUserStep.ContentTemplateContainer.FindControl("txtCaptchaTextBox");
    if (textBox.Text != Session["CaptchaImageText"].ToString())
    {
        // Should not proceed. Go back to Register.aspx and let visitor try again.
        e.Cancel = true;
    }
   TextBox txtUserName = (TextBox)cuwRegister.CreateUserStep.ContentTemplateContainer.FindControl("UserName");
    if (IsUserNameObscene(txtUserName.Text) || (!IsUserNameValid(txtUserName.Text.Trim())))
    {
        DisplayMessage("Please choose a different User name.", "alert");
        e.Cancel = true;
    }

    if (txtUserName.Text.Length < 4)
    {
        DisplayMessage("Please choose a User name more than 4 characters.", "alert");
        e.Cancel = true;
    }
}

private void DisplayMessage(string msg, string css)
{
    // Display message in label in page
}
+1  A: 

I don't think you can use the ASP.NET 2.0 Membership web server controls with the ASP.NET MVC Framework because they rely on the WebForms postback model which isn't supported by MVC.

You can write your own controller and views to provide this functionality. Alternatively, you may want to look at the ASP.NET MVC Membership Starter Kit.

pmarflee
I am not using the Membership control in Mvc but rather wondering how to validate the user name and email address in Mvc. My code above shows how I do it in Web forms. I found ValidateRegistration in AccountController.cs, could this be where it is done?
Picflight
@Picflight: Yes, if you're using the default AccountController, you could use this method to validate the user's registration details. However, you're free to write your own controllers and views to handle Membership functionality if you don't like the ones that come 'out of the box'.
pmarflee

related questions