0

I'm using the following code to create a user account on my vb.net website. The code is in a button click. It works fine as far creating the membership but when the user is redirected to the next page they are not logged in. Apparently "newUser.IsApproved = True" is not working. Any thoughts on what I'm doing wrong. I want the user to be automatically logged in after the membership is created.

      Dim createStatus As MembershipCreateStatus
    Try
        Dim newUser As System.Web.Security.MembershipUser = Membership.CreateUser(txtinput_Email.Value, txtinput_Password.Value, txtinput_Email.Value)
        newUser.IsApproved = True
    Catch ex As Exception
        LabelCreateAccountResults.Text = ex.Message
        ' MessageBox.Show("There's an error: " & vbCrLf & ex.ToString)
        Response.Write("<script language='javascript'>alert('" & ex.Message & "')</script>")
        Exit Sub
    End Try

    Select Case createStatus
        Case MembershipCreateStatus.Success

            LabelCreateAccountResults.Text = "The user account was successfully created!"
            Response.Redirect("yo-delivery.aspx")

        Case MembershipCreateStatus.DuplicateUserName
            LabelCreateAccountResults.Text = "That username already exists."
        Case MembershipCreateStatus.DuplicateEmail
            LabelCreateAccountResults.Text = "A user with that Email address already exists."
        Case MembershipCreateStatus.InvalidEmail
            LabelCreateAccountResults.Text = "PLease enter a VALID email address."
        Case MembershipCreateStatus.InvalidPassword
            LabelCreateAccountResults.Text = "The password entered is invalid. Please enter a passoword with at least 4 characters."
        Case Else
            LabelCreateAccountResults.Text = "Unknown Error: Account NOT created."

    End Select

1 Answer 1

1

OK perhaps look into something along these lines

Dim username__1 As String = Username.Text
Dim password__2 As String = Password.Text

If Membership.ValidateUser(username__1, password__2) Then
    Dim ticket As New FormsAuthenticationTicket(1, username__1, DateTime.Now,      DateTime.Now.AddMinutes(20), False, String.Empty, _
        FormsAuthentication.FormsCookiePath)

    Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)) With { _
        Key .Expires = ticket.Expiration, _
        Key .Path = FormsAuthentication.FormsCookiePath _
    })

    If Not String.IsNullOrEmpty(Request.QueryString("returnurl")) Then
        Response.Redirect(Request.QueryString("returnurl"))
    End If

    Response.Redirect("~/Home")
End If
Sign up to request clarification or add additional context in comments.

7 Comments

The redirect page (yo-delivery.aspx) had the following code in the page load. If Me.User.Identity.IsAuthenticated = False Then Response.Redirect("yo-login.aspx") End If Because the user was not logged in, they were redirected to the login page.
exactly my point, you are only setting IsApproved and in the code you have IsAuthenticated, these are two different things
Thanks but when I first ran in to this problem I thought that the IsApproved and IsAuthenticated might be the problem so I removed the above code and put this: TextBox1.Text = Me.User.Identity.Name.ToString in the yo-delivery.aspx pageload and got nothing (no name) which indicated to me the user is not logged in.
I also should note that when I create a user using a createuserwizard control it works - meaning the user is logged in on the redirect page.
isapproved and isauthenticated are 2 very different things. authenticated means they have performed a log in, approved means they can be created. if you took either out and went to the page then yes there would be no name. set the user to be authenticated then redirect the page and see what happens
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.