dinotom
dinotom

Reputation: 5162

Specified cast error

I am using VS2012 with ASP.NET 4.5 and MySQL as my database provider. I am getting a specified cast error now on code that was working fine at one point. It is from my Register.aspx page for registration verification and the code is adapted from an asp.net website tutorial.

Here is the code, the error is on the Dim newUser as Guid line

Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
    FormsAuthentication.SetAuthCookie(RegisterUser.UserName, False)


    Dim newUser As MembershipUser = Membership.GetUser(RegisterUser.UserName)
    Dim newUserID As Guid = DirectCast(newUser.ProviderUserKey, Guid)
    Dim urlBase As String = Request.Url.GetLeftPart(UriPartial.Authority) & Request.ApplicationPath
    Dim verifyUrl As String = "VerifyNewUser.aspx?ID=" & newUserID.ToString()
    Dim fullPath As String = urlBase & verifyUrl
    Dim appPath As String = Request.PhysicalApplicationPath

    Dim sr As New StreamReader(appPath & "EmailTemplates/VerifyUserAccount.txt")
    Dim mailMessage As New MailMessage()
    mailMessage.IsBodyHtml = True
    mailMessage.From = New MailAddress("[email protected]")
    mailMessage.To.Add(New MailAddress(RegisterUser.Email))
    mailMessage.CC.Add(New MailAddress("[email protected]"))
    mailMessage.Subject = "New User Registration"
    mailMessage.Body = sr.ReadToEnd
    sr.Close()

    mailMessage.Body = mailMessage.Body.Replace("<%UserName%>", RegisterUser.UserName)
    mailMessage.Body = mailMessage.Body.Replace("<%VerificationUrl%>", fullPath)

    //Set up the smtp for gmail to send the email
    Dim mailClient As New SmtpClient()
    With mailClient
        .Port = 587 'try 465 if this doesn't work
        .EnableSsl = True
        .DeliveryMethod = SmtpDeliveryMethod.Network
        .UseDefaultCredentials = False
        .Credentials = New NetworkCredential(mailMessage.From.ToString(), "password")
        .Host = "smtp.gmail.com"
    End With
    mailClient.Send(mailMessage)

    Dim continueUrl As String = RegisterUser.ContinueDestinationPageUrl
    If String.IsNullOrEmpty(continueUrl) Then
        continueUrl = "~/"
    End If

    Response.Redirect(continueUrl)
End Sub

Upvotes: 0

Views: 178

Answers (1)

brendan
brendan

Reputation: 29986

When you, or anyone, creates a MembershipProvider you need to specify whether the ProviderUserKey is going to be an integer or a Guid. The default SqlMembershipProvider implements it a a Guid while the default MySqlMembershipProvider implements it as an int.

You could always implement your own provider by inheriting from one of the defaults and implementing your own version of the ProviderUserKey

Upvotes: 1

Related Questions