ASP.net身份框架 - 重新发送确认电子邮件

时间:2016-06-03 21:12:59

标签: c# asp.net asp.net-mvc asp.net-identity asp.net-identity-2

我为我的ASP.net网站设置了Identity Framework(2?)。我有确认电子邮件,但我无法确定允许用户请求重新发送确认电子邮件的地点或方式。

我发现此部分'重新发送电子邮件确认链接'在this,但这是为MVC写的(我根本不知道)。

有人能指出我正确的方向还是给我一些示例代码?

由于

我正在使用股票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);

3 个答案:

答案 0 :(得分:2)

嗯,这并不是很痛苦。我遗漏了丑陋的部分,我将其最后一个请求的dateTime存储在用户的电话号码字段中。 :)我还没有学习如何将自定义字段添加到aspNetUsers表中。使用dateTime,我可以限制他们要求重新发送的频率......只是因为他们试图发送别人的电子邮件。

    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);
    }

答案 1 :(得分:2)

要将重新发送电子邮件确认添加到ASP.NET core 2. *和3. *中的Identity Framework中,您应通过.Net CLI或Visual Studio搭建代码,如here所示

这是在Visual Studio中执行的操作。

  • 在Visual Studio中,右键单击项目名称,然后选择“添加”>“新建脚手架”项>“身份”。
  • 按“覆盖所有文件”,然后单击“添加”。这将添加最新的nuget软件包。在编写此线程时,它们是Identity.EntityFramworkCore v3.1.3,Identity.UI v3.1.3
  • 转到登录页面。
  • 您可以找到重新发送电子邮件确认链接
  • 从ResendEmailConfirmationModel类中删除抽象

答案 2 :(得分:0)

您可以在管理控制器中使用SendVerificationEmail

有关更多信息,请查看此链接[https://github.com/aspnet/AspNetCore/issues/5410]

相关问题