Question

I'm having some problems getting MembershipUser.createUser to create a user in my database. I don´t get any errors either.

This is the model: All required field are here...

public class RegisterModel
{
    [Required]
    [Display(Name = "Usuário")]
    public string UserName { get; set; }

    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "A {0} deve ter no mínimo {2} caracteres.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Senha")]
    public string Password { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Repetir Senha")]
    [Compare("Password", ErrorMessage = "As senhas não coincidem")]
    public string ConfirmPassword { get; set; }

    [Required]
    [Display(Name = "Pergunta Secreta")]
    public string SecretQuestion { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "A {0} deve ter no mínimo {2} caracteres.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Senha Pergunta Secreta")]
    public string SecretQuestionPassword { get; set; }

    [Required]
    [Display(Name = "Ativo")]
    public bool Active { get; set; }
}

View:

@using (Ajax.BeginForm("Register", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "closeDialog('RegistroUsuario')" }))
{

   <fieldset>
        <legend>Cadastro novo Usuário</legend>
       <table id="changePassword">
                <tr>
                    <td class="smallField">Username:</td>
                    <td>@Html.TextBoxFor(m => m.UserName)</td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td>@Html.PasswordFor(m => m.Password)</td>
                </tr>
                <tr>
                    <td>Repetir Senha:</td>
                    <td>@Html.PasswordFor(m => m.ConfirmPassword)</td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td>@Html.TextBoxFor(m => m.Email)</td>
                </tr>
                <tr>
                    <td>Pergunta Secreta:</td>
                    <td>@Html.TextBoxFor(m => m.SecretQuestion)</td>
                </tr>
                               <tr>
                    <td>Resposta:</td>
                    <td>@Html.TextBoxFor(m => m.SecretQuestionPassword)</td>
                </tr>
                <tr>
                    <td>Ativo:</td>
                    <td>@Html.CheckBox("status")</td>
                </tr>    
            </table>           
    </fieldset>
    <input type="submit" value="Criar Usuário" />
}

Controller: Just a question regarding ProviderUserKey: Is this the right way to get a generic ProviderUserKey?

[HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            MembershipProvider mp  = Membership.Provider;
            MembershipCreateStatus Status;
            object ProviderUserKey = Membership.GetUser().ProviderUserKey;

            // Attempt to register the user
            try
            {
                MembershipUser newUser = mp.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretQuestionPassword, model.Active, ProviderUserKey, out Status);

                if (newUser == null)
                {
                    ViewBag.message = "O Usuário não pode ser cadastrado";
                    return null;
                }
                else
                {
                    return RedirectToAction("Index", "Home");   
                }

            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return PartialView(model);
    }

Thanks a lot in advance.

Was it helpful?

Solution

I found out...

I was failing to create a Guid.

I just changed the controller function for that:

MembershipUser newUser = mp.CreateUser(model.UserName, model.Password, model.Email, model.SecretQuestion, model.SecretQuestionPassword, model.Active, Guid.NewGuid(), out Status);

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top