I have implemented Email confirmation to ASP.Net authentication in "Register" controller in MVC site. Working from MSFT tutorial page referenced in the "Register" method, using the samples of the method and the "SendMail" code to be implemented in "IdentityConfig"
and learning how it works.
I tested in localhost. I got 1) full implementation including
2) redirect to "Info" in shared views ("Email Sent. Please check your Email and confirm....") ,
3) properly formatted Email with link, 4) link/url back to "Accounts/ConfirmEmail" and
5) able to login with new user Email confirmed=true.
Problem: I have now deployed to my production/hosted site. This is on GoDaddy shared hosted site. What happens is:
1) the new user IS posted and written to the AspNetUsers table,
2) I am NOT redirected to "Info" and 3) in Devtools, I am seeing the following:
POST https://docscost.com/Account/Register
500 (Internal Server Error)
...with an Error on the "Register" view. Above error references "Register:1" which is the first line, "<!DOCTYPE html>" with no apparent explanation on what is causing. I admit I am not fully versed on devtools and debugging, but without access to the controller
method in the hosted environment, I have no idea how to find out what is happening. What I have considered and tried are:
1) need for a "<machinekey />" in web.config ?
2) need to add "<httpCookies httpOnlyCookies="true" requireSSL="true" />" to web.config ?
3) ensure that "<allow users="*" />" is added for "Info" in web.config to ensure "Register" can post/redirect to it ?
Here is the code I am using for the "Register" method.
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "<a href=\"" + callbackUrl + "\">Confirm</a>");
ViewBag.Message = "Please check your email and confirm your account to log in.";
return View("Info");
}
AddErrors(result);
}
return View(model);
}
Plz advise if you see anything. I can post the "void SendMail" from IdentityConfig although it seems to only have to do with formatting the email body, naming the email client, etc.
Thanks much,
RC
"Look at it go, Homer; this one's gonna go for miles!"
My best guess is the send email method is failing but it is impossible really know without the error that caused the 500. How about adding error handling and logging so you can get the actual error?
You can also remove (comment) the send email method to see if the error stops. Then you'll know if the email process is causing the exception.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
I got a detailed stack, the first line may give me a clue, highlighting as follows:
[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xx.xx:587]
...where "587" is the port no. For the mail service I am sending from, the smtp tutorial states "Alternatice Port Number:465 (With SSL/TLS)"
so the first thing will be to use their desired port no. 465 and re-compile to see if that's all it is. But your instinct seems right, merely a failure to send email error. I'll do this and report what I see. Thanks again for now!
RC
"Look at it go, Homer; this one's gonna go for miles!"
Thanks but that threw my entire application into server error. Don't know my way around this command in web.config yet, so gonna go with per the other comment for now. Appreciate the help!
RC
"Look at it go, Homer; this one's gonna go for miles!"
Okay, I got past all that. MUCH that I did not understand about my particular setup. I previously had built up MX records on my hosting environment (GoDaddy) for my email server (zoho) but did not realize I should go DIRECT from the hosting smtp as the MX
records are there to handle the "handshake." So trying to use "smtp.zoho.com" as the server did not work at ALL, obviously. Their server either refused connection from the host, or it timed out.
I revised with the appropriate hosting smtp server info, then the email went through fine.
Secondary issue: I have to learn how to get the email to appear in MOST users' inbox (tested w/ my gmail) without a lot of scary SPAM warnings about the email and the "Confirm" link. I initially had an error stating that "secure connections are
not supported" so I had to revise the "SendMail" as follows:
from:
smtpClient.EnableSsl = true;
smtpClient.Send(msg);
To:
smtpClient.EnableSsl = false;
smtpClient.Send(msg);
I will look for any documentation by other users on this, or if you know any such info, plz advise. I'm wondering if the issue is: a) external email server, b) GoDaddy smtp server, or c) settings in my own code/web.config, etc.
Thanks,
RC
"Look at it go, Homer; this one's gonna go for miles!"
Member
39 Points
404 Posts
POST Error on Register
May 06, 2020 07:44 PM|ReidMelSam|LINK
I have implemented Email confirmation to ASP.Net authentication in "Register" controller in MVC site. Working from MSFT tutorial page referenced in the "Register" method, using the samples of the method and the "SendMail" code to be implemented in "IdentityConfig" and learning how it works.
I tested in localhost. I got 1) full implementation including 2) redirect to "Info" in shared views ("Email Sent. Please check your Email and confirm....") , 3) properly formatted Email with link, 4) link/url back to "Accounts/ConfirmEmail" and 5) able to login with new user Email confirmed=true.
Problem: I have now deployed to my production/hosted site. This is on GoDaddy shared hosted site. What happens is: 1) the new user IS posted and written to the AspNetUsers table, 2) I am NOT redirected to "Info" and 3) in Devtools, I am seeing the following:
POST https://docscost.com/Account/Register 500 (Internal Server Error)
...with an Error on the "Register" view. Above error references "Register:1" which is the first line, "<!DOCTYPE html>" with no apparent explanation on what is causing. I admit I am not fully versed on devtools and debugging, but without access to the controller method in the hosted environment, I have no idea how to find out what is happening. What I have considered and tried are:
1) need for a "<machinekey />" in web.config ?
2) need to add "<httpCookies httpOnlyCookies="true" requireSSL="true" />" to web.config ?
3) ensure that "<allow users="*" />" is added for "Info" in web.config to ensure "Register" can post/redirect to it ?
Here is the code I am using for the "Register" method.
Plz advise if you see anything. I can post the "void SendMail" from IdentityConfig although it seems to only have to do with formatting the email body, naming the email client, etc.
Thanks much,
RC
All-Star
53041 Points
23619 Posts
Re: POST Error on Register
May 06, 2020 09:24 PM|mgebhard|LINK
My best guess is the send email method is failing but it is impossible really know without the error that caused the 500. How about adding error handling and logging so you can get the actual error?
You can also remove (comment) the send email method to see if the error stops. Then you'll know if the email process is causing the exception.
Contributor
3730 Points
1420 Posts
Re: POST Error on Register
May 07, 2020 08:16 AM|yij sun|LINK
Hi ReidMelSam,
Accroding to your description,as far as I think,I suggest you could get the details error in web.config,just like this:
Best regards,
Yijing Sun
Member
39 Points
404 Posts
Re: POST Error on Register
May 07, 2020 05:38 PM|ReidMelSam|LINK
Thanks much, Joe...
I got a detailed stack, the first line may give me a clue, highlighting as follows:
[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xx.xx:587]
...where "587" is the port no. For the mail service I am sending from, the smtp tutorial states "Alternatice Port Number: 465 (With SSL/TLS)"
so the first thing will be to use their desired port no. 465 and re-compile to see if that's all it is. But your instinct seems right, merely a failure to send email error. I'll do this and report what I see. Thanks again for now!
RC
Member
39 Points
404 Posts
Re: POST Error on Register
May 07, 2020 05:39 PM|ReidMelSam|LINK
To yij sun:
Thanks but that threw my entire application into server error. Don't know my way around this command in web.config yet, so gonna go with per the other comment for now. Appreciate the help!
RC
All-Star
53041 Points
23619 Posts
Re: POST Error on Register
May 07, 2020 06:14 PM|mgebhard|LINK
There a many reasons for this error; Virus checker, firewall, another process is blocking the port. Contact your hosting provider for assistance.
Member
39 Points
404 Posts
Re: POST Error on Register
May 12, 2020 02:21 PM|ReidMelSam|LINK
Okay, I got past all that. MUCH that I did not understand about my particular setup. I previously had built up MX records on my hosting environment (GoDaddy) for my email server (zoho) but did not realize I should go DIRECT from the hosting smtp as the MX records are there to handle the "handshake." So trying to use "smtp.zoho.com" as the server did not work at ALL, obviously. Their server either refused connection from the host, or it timed out.
I revised with the appropriate hosting smtp server info, then the email went through fine.
Secondary issue: I have to learn how to get the email to appear in MOST users' inbox (tested w/ my gmail) without a lot of scary SPAM warnings about the email and the "Confirm" link. I initially had an error stating that "secure connections are not supported" so I had to revise the "SendMail" as follows:
I will look for any documentation by other users on this, or if you know any such info, plz advise. I'm wondering if the issue is: a) external email server, b) GoDaddy smtp server, or c) settings in my own code/web.config, etc.
Thanks,
RC