【问题标题】:Password Recovery in ASP.NET MVC5ASP.NET MVC5 中的密码恢复
【发布时间】:2015-08-15 14:31:19
【问题描述】:

我正在使用 ASP.NET MVC 5 应用程序。用户可以毫无问题地注册和登录。然而,当一个用户忘记他/她的密码时,忘记密码的过程(已经到位)不会做任何事情!不会向带有click here to reset password 链接的用户发送电子邮件。

目前我的ForgotPassword 操作方法如下所示:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    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");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

我猜这是留给开发人员来实现的。我在 Google 上四处搜索,没有发现任何直截了当的内容。

最简单的方法是什么?

【问题讨论】:

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


    【解决方案1】:

    忘记密码操作以生成重置令牌:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
    {
        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");
            }
    
            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
            var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);      
            await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
            return RedirectToAction("ForgotPasswordConfirmation", "Account");
        }
    
        // If we got this far, something failed, redisplay form
        return View(model);
    }
    

    重置密码操作以根据生成的令牌重置密码:

    [AllowAnonymous]
    public ActionResult ResetPassword(string code)
    {
        return code == null ? View("Error") : View();
    }
    
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null)
        {
            // Don't reveal that the user does not exist
            return RedirectToAction("ResetPasswordConfirmation", "Account");
        }
        var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
        if (result.Succeeded)
        {
            return RedirectToAction("ResetPasswordConfirmation", "Account");
        }
        AddErrors(result);
        return View();
    }
    

    相关视图模型:

    public class ResetPasswordViewModel
    {
        public string Email { get; set; }
        public string Password { get; set; }
        public string ConfirmPassword { get; set; }
        public string Code { get; set; }
    }
    
    public class ForgotPasswordViewModel
    {
        public string Email { get; set; }
    }
    

    但是您需要在发送电子邮件之前配置电子邮件服务。

    public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            return configSendGridasync(message);
        }
    
        private Task configSendGridasync(IdentityMessage message)
        {
            var myMessage = new SendGridMessage();
            myMessage.AddTo(message.Destination);
            myMessage.From = new System.Net.Mail.MailAddress(
                          "you@somewhere.com", "My name");
            myMessage.Subject = message.Subject;
            myMessage.Text = message.Body;
            myMessage.Html = message.Body;
    
            var credentials = new NetworkCredential("userName","Password");
    
            // Create a Web transport for sending email.
            var transportWeb = new Web(credentials);
    
           // Send the email.
           if (transportWeb != null)
           {
               return transportWeb.DeliverAsync(myMessage);
           }
           else
           {
               return Task.FromResult(0);
           }
       }
    }
    

    最后,您需要在用户管理器配置器中注册此类身份,添加以下行:

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
    
        // some code here
    
        manager.EmailService = new EmailService();
    }
    

    请参阅 Account Confirmation and Password Recovery with ASP.NET Identity (C#) 作为分步教程。

    【讨论】:

    • 感谢@SamFarajpourGhamari,我认为await UserManager.SendEmailAsync() 出了点问题,因为我仍然没有收到任何电子邮件!
    • 您还需要配置电子邮件服务。查看我更新的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多