簡體   English   中英

用戶無法恢復其密碼asp.net mvc5

[英]Users can't recover their password asp.net mvc5

如標題所述,如果我的用戶丟失了密碼,便無法恢復。 由於某種原因,它不發送電子郵件。 當他們要確認其電子郵件地址時,我的工作比他們收到的電子郵件有效。

這是該代碼:

    if (ModelState.IsValid)
{
    var user = new ApplicationUser() { UserName = model.Username };
    user.Email = model.Email;
    user.EmailConfirmed = false;
    var result = await UserManager.CreateAsync(user, model.Password);
    if (result.Succeeded)
    {
        MailMessage m = new MailMessage(
            new MailAddress("noreply@stuff.net", "Web Registration"),
            new MailAddress(user.Email));
        m.Subject = "Email confirmation";
        m.Body = string.Format("Dear {0}<BR/>Thank you for your registration, please click on the below link to complete your registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>", user.UserName, Url.Action("ConfirmEmail", "Account", new { Token = user.Id, Email = user.Email }, Request.Url.Scheme));
        m.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("mail.stuff.net");
        smtp.Credentials = new NetworkCredential("noreply@stuff.net", "passwordstuff");
        smtp.EnableSsl = false;
        smtp.Port = 8889;
        smtp.Send(m);
        return RedirectToAction("ConfirmEmail", "Account", new { Email = user.Email });
    }
    else
    {
        AddErrors(result);
    }
}

這是他們想要恢復密碼時的代碼:

            if (ModelState.IsValid)
        {
            var user = await UserManager.FindByNameAsync(model.Email);
            if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
            {
                // Don't reveal that the user does not exist or is not confirmed
                return View("ForgotPasswordConfirmation");
            }

            string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

            MailMessage m = new MailMessage(
            new MailAddress("noreply@stuff.net", "Web Registration"),
            new MailAddress(user.Email));
            m.Subject = "Forgotten Password";
            m.Body = string.Format("Dear {0}<BR/>Please click on the below link to reset your password: <a href=\"{1}\" title=\"User Forgotten Password\">{1}</a>", user.UserName, Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme));
            m.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("mail.stuff.net");
            smtp.Credentials = new NetworkCredential("noreply@stuff.net", "stuff");
            smtp.EnableSsl = false;
            smtp.Port = 8889;
            smtp.Send(m);
            return RedirectToAction("ForgotPasswordConfirmation", "Account");
        }

但是由於某種原因,這是行不通的,他們也不會收到有關如何重設密碼的電子郵件。

假設您正在使用MVC的現成模板,那么:

var user = await UserManager.FindByNameAsync(model.Email);

確實應該是:

var user = await UserManager.FindByEmailAsync(model.Email);

因為輸入框要求輸入電子郵件,而不是用戶名。

然后,除非您將站點設置為需要電子郵件確認,否則該行:

if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))

應該

if (user == null)

這至少會使您了解發送電子郵件的代碼。 我今天遇到了同樣的問題,以上更改為我解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM