skip to Main Content

I was following the "Create a secure ASP.NET MVC 5 web app with log in, email confirmation and password reset (C#)" from Microsoft
I had a hard time setting up the SendGrid Api since i am new to this…
But somehow i got it work to sent me a confirmation email… The Problem is that when i click on the confirmation link it throws me An error occurred while processing your request. Error…***

enter image description here

So this is how i configure SendGrid in the IdentityConfig.cs

 public class EmailService : IIdentityMessageService
    {
         public Task SendAsync(IdentityMessage message)
            {
                return Task.Factory.StartNew(()=> 
                {
                    sendMail(message);
                });
            }
        async void sendMail(IdentityMessage message)
        {
           // var apiKey = ConfigurationManager.AppSettings["SendGridKey"];
            var apiKey = "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";
            var client = new SendGridClient(apiKey);
            var myMessage = new SendGridMessage();
            myMessage.AddTo(message.Destination);
            myMessage.From = new EmailAddress("[email protected]","Angelito");
            myMessage.Subject = message.Subject;
            myMessage.PlainTextContent = message.Body;
            await client.SendEmailAsync(myMessage);
        }
    }

i also runned the application in debug mode, and add it a break point to the Register Method and ConfirmEmail Method. In the ConfirmEmail Method i got this Error. i am guessing it has to be something with the Token…"

enter image description here

If Anyone could help me fix this i would really apreciated…
Also if you guys could recommend latest books to Become a pro at asp.net or core. i would much apreciated.

So i made the changes to the code… Am still receiving the the confirmation link on my Email but when i click on it get invalidToken if i add a break point to ConfirmEmail Method…

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Okay so here is what the code looks like inside IdentityConfig.cs in order to send an Email confirmation and to confirm it in your inbox. The following code worked for me. The rest is the same as the Microsoft.docs

    IdentityConfig.cs

         public async Task SendAsync(IdentityMessage message)
            {
            await configSendGridasync(message);
            }
        private async Task configSendGridasync(IdentityMessage message)
        {
            var apiKey = "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";
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("[email protected]","Angel" );
            var subject = message.Subject;
            var to = new EmailAddress(message.Destination);
            var plainTextContent = message.Body;
            var htmlContent = message.Body;
            var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent,htmlContent);
    
            // Send the email.
            if (client != null)
            {
                await client.SendEmailAsync(msg);
            }
            else
            {
                Trace.TraceError("Failed to create Web transport.");
                await Task.FromResult(0);
            }
        }
    

  2. The problem is in the code. You shouldn’t be using async void or using Task.Factory.StartNew. async void is only meant for asynchronous event handlers and can’t be awaited.

    Since SendEmailAsync is asynchronous, there’s no reason to run it into another task. Even if there was a reason to start another task, Task.Run should be used, not Task.Factory.StartNew.

    The code should change to :

    public Task SendAsync(IdentityMessage message)
    {
        await sendMailAsync(message);
    }
    
    async Task sendMailAsync(IdentityMessage message)
    {
        ...
        await client.SendEmailAsync(myMessage);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search