user1375002
user1375002

Reputation: 103

Automatically Logging In User After Custom Membership Creation (not using Wizard)

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

Upvotes: 0

Views: 41

Answers (1)

bensonsearch
bensonsearch

Reputation: 312

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

Upvotes: 1

Related Questions