0

データベースにユーザーを作成するために MembershipUser.createUser を取得する際に問題が発生しています。エラーも発生しません。

これがモデルです: 必須フィールドはすべてここにあります...

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; }
}

意見:

@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" />
}

コントローラー: ProviderUserKey に関する質問: これは、一般的な 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);
    }

よろしくお願いします。

4

1 に答える 1

1

分かった...

Guid の作成に失敗していました。

そのためのコントローラー関数を変更しました:

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

ありがとう

于 2012-11-19T18:40:58.720 に答える