0

我将 VS2012 与ASP.NET 4.5MySQL的数据库提供程序一起使用。我现在在某个时间运行良好的代码上收到一个指定的转换错误。它来自我用于注册验证的 Register.aspx 页面,代码改编自 asp.net 网站教程。

这是代码,错误就Dim newUser as Guid行了

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("myacct@gmail.com")
    mailMessage.To.Add(New MailAddress(RegisterUser.Email))
    mailMessage.CC.Add(New MailAddress("myacct@gmail.com"))
    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
4

1 回答 1

1

当您或任何人创建 a 时,MembershipProvider您需要指定ProviderUserKey是整数还是 Guid。默认实现它是一个SqlMembershipProviderGuid,而默认MySqlMembershipProvider实现它是一个 int。

您始终可以通过从默认值之一继承并实现自己的版本来实现自己的提供程序ProviderUserKey

于 2012-09-04T02:23:18.683 回答