none
Gerar excessao na criacao de usuario RRS feed

  • Pergunta

  • Pessoal,

    Qdo vou criar um usuario e ele ja existe, o sistema me gera uma excessao.

    No entanto quando uso a sintaxe abaixo, ele nao gera.
    Pq isso acontece, por causa do atributo OUT ?

    MembershipUser newUser = Membership.CreateUser(user, "senha", user, "", "", true, out createStatus);

    Abracos

    sexta-feira, 15 de junho de 2012 16:34

Respostas

  • Porque ele atribui o erro no enum MembershipCreateStatus (no caso o seu objeto que fui atribuido no OUT), ele que deve ser validado para lançar a exceção, veja este exemplo da msdn:

    public void CreateUser_OnClick(object sender, EventArgs args)
    {
      // Create new user and retrieve create status result.
    
      MembershipCreateStatus status;
      string passwordQuestion = "";
      string passwordAnswer = "";
    
      if (Membership.RequiresQuestionAndAnswer)
      {
        passwordQuestion = PasswordQuestionTextbox.Text;
        passwordAnswer = PasswordAnswerTextbox.Text;
      }
    
      try
      {
        MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text, 
                                                       EmailTextbox.Text, passwordQuestion,
                                                       passwordAnswer, true, out status);
        if (newUser == null)
        {
          Msg.Text = GetErrorMessage(status);
        }
        else
        {
          Response.Redirect("login.aspx");
        }
      }
      catch
      {
        Msg.Text = "An exception occurred creating the user.";
      }
    }
    
    public string GetErrorMessage(MembershipCreateStatus status)
    {
       switch (status)
       {
          case MembershipCreateStatus.DuplicateUserName:
            return "Username already exists. Please enter a different user name.";
    
          case MembershipCreateStatus.DuplicateEmail:
            return "A username for that e-mail address already exists. Please enter a different e-mail address.";
    
          case MembershipCreateStatus.InvalidPassword:
            return "The password provided is invalid. Please enter a valid password value.";
    
          case MembershipCreateStatus.InvalidEmail:
            return "The e-mail address provided is invalid. Please check the value and try again.";
    
          case MembershipCreateStatus.InvalidAnswer:
            return "The password retrieval answer provided is invalid. Please check the value and try again.";
    
          case MembershipCreateStatus.InvalidQuestion:
            return "The password retrieval question provided is invalid. Please check the value and try again.";
    
          case MembershipCreateStatus.InvalidUserName:
            return "The user name provided is invalid. Please check the value and try again.";
    
          case MembershipCreateStatus.ProviderError:
            return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
    
          case MembershipCreateStatus.UserRejected:
            return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
    
          default:
            return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
       }
    }

    Referencia:
    http://msdn.microsoft.com/pt-br/library/82xx2e62.aspx

    Vitor Mendes | Seu feedback é muito importante para todos!
    Visite o meu site: http://www.vitormendes.com.br/


    • Editado Vitor Mendes sexta-feira, 15 de junho de 2012 16:43
    • Sugerido como Resposta Danimar Ribeiro sexta-feira, 15 de junho de 2012 21:48
    • Marcado como Resposta AGA Neto sexta-feira, 22 de junho de 2012 16:48
    sexta-feira, 15 de junho de 2012 16:42