I want to allow users create an account in my website, Need to validate their email address, so after registration, an email will be sent with a confirmation link. confirmation link looks something like that :
https://localhost:44326/Identity/Account/ConfirmEmail?userId=b7160222-e3eb-4315-9e01-cc3ba34cc2c1&code=Q2ZESjhG...
when i click the link i get to function
public async Task<IActionResult> OnGetAsync(string userId, string code)
in confirmEmail.cshtml.cs
The userId is populated but the code is null.
Any ideas why code field is null?
Thanks
CodePudding user response:
My guess is that your & is not decoded... but it may be your copy-pasting here that is the problem. What is the URL that is in the address of the browser? & or &?
CodePudding user response:
Your code uses & to join the parameters which is not proper URL encoding.
It isn't necessary to replace & with & in your url since & is an HTML literal and won't be understood in the request processing.
When you generate the confirmation link, you need to URL encode it. The output of the encoding should look something like
https%3A%2F%2Flocalhost%3A44326%2FIdentity%2FAccount%2FConfirmEmail%3FuserId%3Db7160222-e3eb-4315-9e01-cc3ba34cc2c1%26code%3DQ2ZESjhG
When building the link, it should simply be a case of using the correct characters:
var confirmLink = "https://localhost:4326/Identity/Account/ConfirmEmail?userId=xyz&code=abc";
If you url encode that string you should be good.
