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…***
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…"
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…
2
Answers
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
The problem is in the code. You shouldn’t be using
async void
or usingTask.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, notTask.Factory.StartNew
.The code should change to :