Search code examples
c#asp.net-coreemail-verification

How can I create a random unique token and use it to verify an email address for a user in ASP.NET Core?


I have all the logic written for my user registration except for email verification. Via my researched I learned that, simplifyingly, I need to create a random unique token and include that token in a link sent to the email address that is to be verified.

How can I create this random token and what how should I process the request from the activation link?


Solution

  • I don't know about Nuget but you can code yourself for sending an activation link and processing it. The steps are:

    1) Generate random unique token and save it in your database. For generating unique token you can use UUID in Java as

    String uniqueID = UUID.randomUUID().toString();
    

    2) Include that token, encrypt it (not necessary though) and send it to the email ID.

    example: www.xyz.com/activation?action=a273jsjh2718sjhdj271jgsdjaj28jh

    3) When ever the user clicks that link, invoke the method for further processing in your controller. Map the url to your method in your controller.

    Here is the example done using Spring Framework. I hope it helps.

    PS: I don't know whether it is good practice for production. I just tried to help you.