通过电子邮件发送的密码重置链接

时间:2018-01-24 20:01:55

标签: c# asp.net-core-mvc

创建将发送到电子邮件,执行密码交换的验证链接的最佳方法是什么?我已经将所有发送逻辑都工作了,但是我需要添加此链接,该链接将被重定向到View(RedefinicaoSenha)以更改密码。

按照送货方式。

public void EnviarEmail()
        {
            try
            {
                //From Address  
                string FromAddress = _config.GetValue<string>("From");
                string FromAdressTitle = "TesteEnvio";
                //To Address  
                string ToAddress = CliEmail;
                string ToAdressTitle = "Microsoft ASP.NET Core";
                string Subject = "Redefinição de senha";//Assunto,
                string BodyContent = "Prezado(a)  para redefinir sua senha, por favor clique no link abaixo.";

                //Smtp Server  
                string SmtpServer = _config.GetValue<string>("SmtpServer");
                //Smtp Port Number  
                int SmtpPortNumber = _config.GetValue<Int32>("Port");

                var mimeMessage = new MimeMessage();
                mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
                mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
                mimeMessage.Subject = Subject;
                mimeMessage.Body = new TextPart("plain")
                {
                    Text = BodyContent

                };

                using (var client = new SmtpClient())
                {
                    client.Connect(SmtpServer, SmtpPortNumber, false);   
                    client.Authenticate(_config.GetValue<string>("Usuario"), _config.GetValue<string>("Password"));
                    client.Send(mimeMessage);
                    client.Disconnect(true);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

1 个答案:

答案 0 :(得分:2)

您可以使用ASP.NET Identity框架。它将为您处理重置密码令牌生成。

类似的东西:

public static class Helper
{
    static BigInteger[] factors = Enumerable.Range(0, 19).Select(i=> BigInteger.Pow(10, i)).ToArray();
    public static BigInteger ParseFast(string str)
    {
        var result = new BigInteger(0);
        var n = str.Length;
        var hasSgn = str[0] == '-';
        int j;
        for (var i = hasSgn ? 1 : 0; i < n; i += j - i)
        {
            long gr = 0;
            for (j = i; j < i + 18 && j < n; j++)
            {
                gr = gr * 10 + (str[j] - '0');
            }
            result = result * factors[j-i]+ gr;

        }
        if (hasSgn)
        {
            result = BigInteger.MinusOne * result;
        }
        return result;
    }
}

您可以在此处找到更多信息:

Account Confirmation and Password Recovery with ASP.NET Identity (C#)