ASP.net Identity Framework - Resend Confirmation Email
Asked Answered
B

3

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);
Brenza answered 3/6, 2016 at 21:12 Comment(4)
Can you clarify or show your code? Is the email confirmation for registering on the site? Are you using forms Authentication?Solubilize
@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?Brenza
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 valueSolubilize
@Solubilize 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 frameworkBrenza
B
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);
    }
Brenza answered 4/6, 2016 at 4:5 Comment(0)
C
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.
Camlet answered 6/6, 2020 at 18:37 Comment(1)
This worked for me, deleted "abstract" qualifier from the ResendEmailConfirmationModel "code-behind-page" class.Heel
P
1

You can use SendVerificationEmail in the Manage controller.

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

Philps answered 25/5, 2019 at 5:53 Comment(1)
I was using webforms at the time, not mvc. But thanksBrenza

© 2022 - 2024 — McMap. All rights reserved.