2012-11-19 46 views
0

我在使用MembershipUser.createUser在我的數據庫中創建用戶時遇到了一些問題。我也沒有得到任何錯誤。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); 
    } 

非常感謝。

回答

0

我找到了...

我沒有創建一個Guid。

我只是改變了控制器功能:

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

感謝