Reputation: 554
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);
Upvotes: 11
Views: 10145
Reputation: 458
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.
Upvotes: 2
Reputation:
You can use SendVerificationEmail
in the Manage controller.
For more info, check out this link https://github.com/aspnet/AspNetCore/issues/5410
Upvotes: 1
Reputation: 554
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);
}
Upvotes: 3