Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to set up ASP.Net identity in a webform. When the user fills out the registeration page the email is sent but it is empty. Here is my Create User_Click. Did I miss something
VB.NET
Protected Sub CreateUser_Click(sender As Object, e As EventArgs)
    Dim userName As String = Email.Text
    Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
    Dim signInManager = Context.GetOwinContext().Get(Of ApplicationSignInManager)()
    Dim user = New ApplicationUser() With {.UserName = userName, .Email = userName}
    Dim result As IdentityResult = manager.Create(user, Password.Text)
    If result.Succeeded Then
        ' For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
        Dim code As String = manager.GenerateEmailConfirmationToken(user.Id)
        Dim callbackUrl As String = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request)
        manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href="">here</a>.")
        If user.EmailConfirmed Then
            signInManager.SignIn(user, isPersistent:=True, rememberBrowser:=False)
            IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response)
        Else
            ErrorMessage.Text = "An email has been sent to your account. Please view the email and confirm your account to complete the registration process."
        End If
    Else
        ErrorMessage.Text = result.Errors.FirstOrDefault()
    End If
End Sub
VB.NET
Public Class EmailService
    Implements IIdentityMessageService
    Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
        ' Plug in your email service here to send an email.

        Dim mm As New MailMessage()
        mm.From = New MailAddress("emailaddress", "name")
        mm.To.Add(New System.Net.Mail.MailAddress(message.Destination))

        mm.Subject = "Account Created for COSI Data Reporting"
        mm.Body = ""
        mm.IsBodyHtml = True


            Dim smtp As New SmtpClient()
            smtp.Host = "##.##.###.##"
            smtp.Port = "###"
            smtp.Credentials = New System.Net.NetworkCredential("username", "password")
        ' mm.To.Add(New System.Net.Mail.MailAddress()


        smtp.Send(mm)

        Return Task.FromResult(0)


    End Function
End Class


What I have tried:

I have researched but found nothing specific to this.
Posted
Updated 29-Jan-20 5:08am
v3
Comments
Richard Deeming 29-Jan-20 10:32am    
You've presumably created an IIdentityMessageService implementation to send an email, and set the UserManager's EmailService to an instance of that class?

What does the code in that class look like?
rlgentry 29-Jan-20 11:01am    
Yes, I set the IIDentityMessageService. Below is the code. I am using SMTP. I changed nothing else on the Identityconfig.vb page

Public Class EmailService
Implements IIdentityMessageService
Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
' Plug in your email service here to send an email.

Dim mm As New MailMessage()
mm.From = New MailAddress("emailaddress", "name")
mm.To.Add(New System.Net.Mail.MailAddress(message.Destination))

mm.Subject = "Account Created for COSI Data Reporting"
mm.Body = ""
mm.IsBodyHtml = True


Dim smtp As New SmtpClient()
smtp.Host = "##.##.###.##"
smtp.Port = "###"
smtp.Credentials = New System.Net.NetworkCredential("username", "password")
' mm.To.Add(New System.Net.Mail.MailAddress()


smtp.Send(mm)

Return Task.FromResult(0)


End Function
End Class
Richard Deeming 29-Jan-20 11:08am    
Has the site swallowed some of your code, or does your EmailService class really set the message body to an empty string?
rlgentry 29-Jan-20 11:32am    
I'm not sure what you mean by that. When I debug with the breakpoint set at Sendasyc function in the IdentityConfig file, the body is filled out and everything is hit.

When I set the breakpoint in register, the callbackUrl is a code of some sort. is that correct?
Richard Deeming 29-Jan-20 11:35am    
The code you posted has:
mm.Body = ""

If it's set correctly when you debug, then the site must have swallowed some of your code.

The callbackUrl should be a link back to your site with a confirmation code appended, which will let the site know that the email address provided by the user is valid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900