11

I'm setting Identity Framework (2?) for my ASP.net site. I have the confirmation email working, but I can't figure out where or how to allow the user to request a resend of the confirmation email.

I found this section 'Resend email confirmation link' in this, but that is written for MVC (which I don't know much at all).

Could someone point me in the right direction or throw me some sample code?

Thanks

I'm using the stock Identity Framework.

string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);

manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");

signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
Gregory Mertens
  • 534
  • 1
  • 8
  • 15
  • Can you clarify or show your code? Is the email confirmation for registering on the site? Are you using forms Authentication? – DaniDev Jun 03 '16 at 21:56
  • @DaniDev. I added a snippet from the stock code. But that is where the email is sent from when they first register. Is it safe for me to create my own page with a similar call? – Gregory Mertens Jun 03 '16 at 22:12
  • 1
    It should be, if you are able to invoke the required objects Identityhelper, signinManager. you also have to be able to locate/retrieve the 'code' parameter value – DaniDev Jun 03 '16 at 22:35
  • @DaniDev Thanks. I'll try. I'm a little worried that I am approaching this wrong since I wasn't able to find anyone else with the same problem. Surely people are losing their confirmation emails from time to time. It is also worrying that Microsoft didn't include an option for re-sending in their framework – Gregory Mertens Jun 03 '16 at 22:46

3 Answers3

3

Well that didn't turn out to be very painful. I left out the ugly part where I am storing the dateTime of their last request inside of the user's phone number field. :) I haven't learned how to add custom fields to the aspNetUsers table yet. With the dateTime I can limit how often they ask for a resend...just incase they are trying to spam someone else's email.

    private ApplicationUser _currentUser;
    private ApplicationUserManager _manager;

    protected ApplicationUserManager Manager
    {
        get { return _manager ?? (_manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>()); }
    }

    protected ApplicationUser CurrentUser
    {
        get { return _currentUser ?? (_currentUser = Manager.FindById(User.Identity.GetUserId())); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (CurrentUser == null || !User.Identity.IsAuthenticated)
        {
            Response.Redirect("~/account/register.aspx");
        }
        else if (User.Identity.IsAuthenticated && CurrentUser.EmailConfirmed)
        {
            alreadyConfirmed.Visible = true;
        }
        else if (!minTimeElapsedSinceLastRequest())
        {
            NotEnoughTimeLiteral.Text = "A resend occurred on " + CurrentUser.PhoneNumber + ". Please wait longer before your next request";
            notEnoughTimeFlag.Visible = true;
        }
        else
        {
            idResendButton.Enabled = true;
        }
    }

    protected void ResendConfirmationEmailClick(object sender, EventArgs e)
    {
        string currentUserId = User.Identity.GetUserId();

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
        string code = Manager.GenerateEmailConfirmationToken(currentUserId);
        string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, currentUserId, Request);

        Manager.SendEmail(currentUserId, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");
        setUsersLastResendDateTime(CurrentUser);
        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
    }
Gregory Mertens
  • 534
  • 1
  • 8
  • 15
2

To add Resend Email Confirmation to Identity Framework in ASP.NET core 2.* and 3.* you should scaffold the code either through .Net CLI or Visual Studio as shown here.

This is how to do it in visual studio.

  • In visual studio right-click on project name and choose Add>New Scaffolded item>Identity.
  • Press Override all files then click Add. This adds the latest nuget packages. At the time of writing this thread they are Identity.EntityFramworkCore v3.1.3, Identity.UI v3.1.3
  • Go to login page.
  • you can find Resend Email Confirmation link
  • Remove abstract from ResendEmailConfirmationModel class.
Hamed
  • 356
  • 2
  • 9
  • This worked for me, deleted "abstract" qualifier from the ResendEmailConfirmationModel "code-behind-page" class. – Kris Bunda Sep 15 '20 at 19:42
1

You can use SendVerificationEmail in the Manage controller.

For more info, check out this link https://github.com/aspnet/AspNetCore/issues/5410

ChrisO
  • 5,068
  • 1
  • 34
  • 47