Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net Identity Framework - Resend Confirmation Email

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);
like image 444
Gregory Mertens Avatar asked Jun 03 '16 21:06

Gregory Mertens


3 Answers

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.
like image 87
Hamed Avatar answered Nov 14 '22 06:11

Hamed


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);
    }
like image 3
Gregory Mertens Avatar answered Nov 14 '22 05:11

Gregory Mertens


You can use SendVerificationEmail in the Manage controller.

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

like image 1
Jatin Kumar Avatar answered Nov 14 '22 04:11

Jatin Kumar