2 years ago
#17318
Ethan Partridge
"Antiforgery token could not be decrypted" when trying to connect to SMTP server in ASP.NET Core 5 MVC
I have an ASP.NET Core 5 MVC web application. I'm attempting to send an email whenever users fill out my contact form. I'm using MailKit to send emails and trying to connect to a NameCheap (Private Email) SMTP email server. I keep getting the error:
022-01-05T21:33:53.7036238-06:00 800023e1-0c0e-a700-b63f-84710c7967bb [ERR] An exception was thrown while deserializing the token. (348bf365)
Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery token could not be decrypted.
---> System.Security.Cryptography.CryptographicException: The key {56726e51-68ea-4ae3-ac4d-13a488d4f33e} was not found in the key ring.
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgeryTokenSerializer.Deserialize(String serializedToken)
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.GetCookieTokenDoesNotThrow(HttpContext httpContext)
2022-01-05T21:34:26.6022697-06:00 80002108-0804-fc00-b63f-84710c7967bb [ERR] System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10013): An attempt was made to access a socket in a way forbidden by its access permissions. 66.29.159.53:587
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at MailKit.Net.SocketUtils.ConnectAsync(String host, Int32 port, IPEndPoint localEndPoint, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.SocketUtils.ConnectAsync(String host, Int32 port, IPEndPoint localEndPoint, Int32 timeout, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.MailService.ConnectNetwork(String host, Int32 port, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.ConnectAsync(String host, Int32 port, SecureSocketOptions options, Boolean doAsync, CancellationToken cancellationToken)
at MailKit.Net.Smtp.SmtpClient.Connect(String host, Int32 port, SecureSocketOptions options, CancellationToken cancellationToken)
at Portfolio.Controllers.HomeController.Message(FormModel formObj) in C:\Users\ethan\source\repos\Portfolio\Portfolio\Controllers\HomeController.cs:line 340 (1462bf77)
I've already contacted NameCheap customer support, and the host and port are correct, so I'm not sure what the problem could be.
My code:
var email = new MimeMessage();
email.From.Add(new MailboxAddress([Sender Name], "[Sender Email]"));
email.To.Add(new MailboxAddress("[Receiver Name]", "[Receiver Email]"));
email.Subject = "Message from: " + [@Model.Name];
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "<b>Name: </b>" + [@Model.Name] + "<br/>"
+ "<b>Email: </b>" + [@Model.Email] + "<br/>"
+ "<b>Message:</b>" + "<br/>" + [@Model.Message];
email.Body = bodyBuilder.ToMessageBody();
var client = new SmtpClient();
try
{
client.Connect([Host], 587, SecureSocketOptions.SslOnConnect);
client.Authenticate([Sender Email], [Sender Password]);
client.Send(email);
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
_logger.LogError("Email Sent = [Name: " + [@Model.Name] + "], "
+ "[Email: " + [@Model.Email] + "], "
+ "[Message: " + [@Model.Message] + "]");
}
finally
{
client.Disconnect(true);
client.Dispose();
}
c#
asp.net-core
mailkit
antiforgerytoken
0 Answers
Your Answer