Usuário com melhor resposta
Membership CreateUser

Pergunta
-
Pessoal,
Estou criando um usuario no membership, porem ocorre o erro "The E-mail supplied is invalid."
O email ta vindo como "teste@teste.com".MembershipUser newUser = Membership.CreateUser(UserName.Text.Trim(), "senhaPadra0");
Roles.AddUserToRole(UserName.Text.Trim(), "Usuarios");Alem disso, como faco pra checar se o email ja esta cadastrado antes de cria-lo ?
Abracos
Respostas
-
Neto se você tenta cadastra um usuário caso o e-mail já exista, o membership dispara um exception, para ser mais especifico o execption MembershipCreateUserException, tente fazer uma rotina parecida com essa para validar este erro:
public void CreateUser_OnClick(object sender, EventArgs args) { try { // Create new user. MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text); // If user created successfully, set password question and answer (if applicable) and // redirect to login page. Otherwise return an error message. if (Membership.RequiresQuestionAndAnswer) { newUser.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text, PasswordQuestionTextbox.Text, PasswordAnswerTextbox.Text); } Response.Redirect("login.aspx"); } catch (MembershipCreateUserException e) { Msg.Text = GetErrorMessage(e.StatusCode); } catch (HttpException e) { Msg.Text = e.Message; } } 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/en-us/library/d8t4h2es.aspx
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/
Todas as Respostas
-
Olá,
Veja se ajuda:
http://weblogs.asp.net/scottgu/archive/2006/05/07/ASP.NET-2.0-Membership-and-Roles-Tutorial-Series.aspx
Vídeo passo a passo: http://www.youtube.com/watch?v=vxxFhGF-Z7E
Abraço
HJ- Sugerido como Resposta hamiltonj segunda-feira, 7 de novembro de 2011 12:54
-
Neto se você tenta cadastra um usuário caso o e-mail já exista, o membership dispara um exception, para ser mais especifico o execption MembershipCreateUserException, tente fazer uma rotina parecida com essa para validar este erro:
public void CreateUser_OnClick(object sender, EventArgs args) { try { // Create new user. MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text); // If user created successfully, set password question and answer (if applicable) and // redirect to login page. Otherwise return an error message. if (Membership.RequiresQuestionAndAnswer) { newUser.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text, PasswordQuestionTextbox.Text, PasswordAnswerTextbox.Text); } Response.Redirect("login.aspx"); } catch (MembershipCreateUserException e) { Msg.Text = GetErrorMessage(e.StatusCode); } catch (HttpException e) { Msg.Text = e.Message; } } 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/en-us/library/d8t4h2es.aspx
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/ -
Provavelmente já existe um usuario com este e-mail! para criar varios usuários com o mesmo e-mail é prciso
configurar o web.config assim.<membership defaultProvider="SqlProvider"> <providers> <add connectionStringName="ConnectionString" applicationName="nome aplicação" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="false" passwordFormat="Encrypted" maxInvalidPasswordAttempts="5" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="5" name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" /> </providers> </membership>
A configuração do membership acima permite ser cadastrado usuários com o mesmo e-mail, agora para verificar se o e-mail já existe eu sugiro que crie uma rotina para verificar na base de dados da tabela membership se já existe o e-mail que esta sendo cadastrdo, essa rotina seria chamada no onchange do campo txt.
- Editado Romy Moura quarta-feira, 2 de novembro de 2011 23:14
- Sugerido como Resposta hamiltonj segunda-feira, 7 de novembro de 2011 12:54
-
Neto se você tenta cadastra um usuário caso o e-mail já exista, o membership dispara um exception, para ser mais especifico o execption MembershipCreateUserException, tente fazer uma rotina parecida com essa para validar este erro:
Curti a classe! muito util mesmo!
public void CreateUser_OnClick(object sender, EventArgs args) { try { // Create new user. MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text); // If user created successfully, set password question and answer (if applicable) and // redirect to login page. Otherwise return an error message. if (Membership.RequiresQuestionAndAnswer) { newUser.ChangePasswordQuestionAndAnswer(PasswordTextbox.Text, PasswordQuestionTextbox.Text, PasswordAnswerTextbox.Text); } Response.Redirect("login.aspx"); } catch (MembershipCreateUserException e) { Msg.Text = GetErrorMessage(e.StatusCode); } catch (HttpException e) { Msg.Text = e.Message; } } 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/en-us/library/d8t4h2es.aspx
Vitor Mendes | Seu feedback é muito importante para todos!
Visite o meu site: http://www.vitormendes.com.br/ -
Amigo, como ficou está questão ? a resposta do colega lhe ajudou a resolver o problema ?
Obrigado
Olavo Oliveira Neto
http://olavooneto.wordpress.com
Twitter @Olavooneto
Se for útil marque como resposta e faça um Developer feliz :)