是否有人使用自定义令牌进行电子邮件验证,而之前不必使用 websecurity.CreateAccount 方法?
当然 - 我们为所有电子邮件验证令牌执行此操作,这是我们“创建帐户”流程的一部分。你可以做这样的事情来生成令牌:
public static string GenerateEmailToken()
{
// generate an email verification token for the user
using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider())
{
byte[] data = new byte[16];
provider.GetBytes(data);
return Convert.ToBase64String(data);
}
}
然后我将其包含在我们的“CreateAccountResponse”中,这允许控制器将其提取并发送包含令牌的验证电子邮件 - 给您一个想法:
public class CreateAccountResponse
{
public bool CreatedSuccessfully { get; set; }
public string EmailVerificationToken { get; set; }
}
然后我们在这里处理:
CreateAccountResponse response = _mediator.Send(createAccountCommand);
if (response.CreatedSuccessfully)
{
if (!string.IsNullOrEmpty(response.EmailVerificationToken))
{
// Send the verification email if a token is present
SendVerificationEmail(model.Email, response.EmailVerificationToken);
}
return RedirectToAction("Settings", "Account");
}
然后使用令牌,我们创建完整格式的 URL 以包含在您发送给该用户的电子邮件中
private void SendVerificationEmail(string emailAddress, string token)
{
try
{
// Url.Action will encode parameters as they are needed.
var verificationUrl = Url.Action("VerifyAccount", "Account", new { token = token }, Request.Url.Scheme);
_userMailer.DeliverVerification(emailAddress, verificationUrl);
}
catch (Exception ex)
{
_logger.ErrorLog.Log(new VerificationError(ex));
}
}
一旦他们点击电子邮件中的链接,我们的“VerifyAccount”路线就会获取令牌并允许我们完成其余的验证过程。
此外,出于安全目的,非常好为您的验证令牌设置一个到期 - 24 到 48 小时通常是允许用户接受的时间范围要接收电子邮件,请点击链接并验证他们的帐户。