Welcome to WebmasterWorld Guest from 34.236.145.124

Forum Moderators: ocean10000

Message Too Old, No Replies

Problem with Adding a new User

     
1:25 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


I am using the membership provider and I have created my own add a user page. However, my add a user code is not working properly. Here is my vb.

Imports System.Web.Security

Partial Public Class AddUser
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

'if they click the add button
Private Sub addUser_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles addUser.Click

'all of the input from the user
Dim username As String = userNameInput.Text
Dim password As String = passwordInput.Text
Dim password2 As String = passwordInput2.Text
Dim email As String = emailInput.Text
Dim question As String = questionInput.Text
Dim answer As String = answerInput.Text
Dim success As Boolean = True

'check to make sure they entered a valid email
If (Utilities.IsValidEmail(email)) Then

'check to make sure the passwords match
If (password = password2) Then

'the user object
Dim newUser As MembershipUser

'try to create the new user
Dim result As MembershipCreateStatus
Try

newUser = Membership.CreateUser(username, password, email, question, answer, True, result)

Catch ex As MembershipCreateUserException
output.Text = GetErrorMessage(ex.StatusCode)
success = False
Catch ex As HttpException
output.Text = ex.Message
success = False
End Try

'if this is true then the memeber was created
'as of now we are only using one role
'so we just assing the role of super admin.
If (success) Then
Response.Write(newUser.UserName)

End If 'if success

Else 'the passwords didn't match

output.Text = "<span class='textRed'>Your passwords did not match.</span>"

End If 'if password = password2

Else 'the email was invalid

output.Text = "<span class='textRed'>Please enter a valid email.</span>"

End If 'if isvalid email

End Sub 'add user click

'this function returns the proper error message if the create user fails
Private Function GetErrorMessage(ByVal status As MembershipCreateStatus) As String

Select Case 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."

Case Else
Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
End Select
End Function

End Class

I have noticed that when I uncomment the response.write line I get a null reference error meaning that newUser is nothing. I have no idea what I am doing wrong. Any suggestions? Thanks,

1:43 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


What is the value of 'result'? (I see that you have declared it and set it's value, but it's not being used anywhere)

You should be using this result instead of a try/catch block (see my earlier post [webmasterworld.com])

1:53 pm on Aug 24, 2009 (gmt 0)

Administrator

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month

joined:Jan 14, 2004
posts:864
votes: 3


I can not tell whats wrong with your code but it looks all right so only thing I see you are not doing is checking the return result if it says it is a Sucessor not. I am guessing it is failing but not throwing an exception. (My vb skill are rusty, I typed this in free form)

If (success) Then
If (result <> MembershipCreateStatus.Success) then
output.Text = GetErrorMessage(result)
ELSE
Response.Write(newUser.UserName)
END IF
End If 'if success

2:01 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


In the documentation I found, it asks for that number of arguments. It says it needs a ByRef status as an object for the last argument. I just copied this off of the msdn website. I'm not for sure how I would do it different. Do you have any suggestions? Thanks,
2:07 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


Yes, unfortunately the MSDN docs here aren't that great...

Try adding the following:
newUser = Membership.CreateUser(username, password, email, question, answer, True, result)
Response.Write(result.ToString())

2:10 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


Because this overload of Membership.CreateUser shouldn't generate an exception under normal circumstances, querying result should suffice:

Dim result As MembershipCreateStatus
newUser = Membership.CreateUser(username, password, email, question, answer, True, result)

Select Case result
Case MembershipCreateStatus.DuplicateEmail
' Dupe Email
Case MembershipCreateStatus.DuplicateUserName
' Dupe User Name
... etc. etc.
End Select

2:30 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Ok so I found the problem. Thanks marcel for the idea of the result.tostring that is what led me to my problem. I was not providing an long enough password (because I was just using the word test). My question is, how come the function GetErrorMessage did not catch that as an error? Thanks,
2:32 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


how come the function GetErrorMessage did not catch that as an error?

Because this overload of Membership.CreateUser does not generate an exception, it's caught in MembershipCreateStatus ('result' in this case)

*Edit - As I said, the MSDN documentation here could be better :)

2:41 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 10, 2008
posts: 1130
votes: 0


Does anyone know where the documentation on the error messages for the createuserstatus so I can display different messages based on the error? Thanks,
2:43 pm on Aug 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:732
votes: 0


Here you go: [msdn.microsoft.com...]

But Intellisense will also help you out here.