GeneratePasswordToken在MVC 5中抛出奇怪的错误(asp.net身份) - 忘记密码

时间:2014-08-22 15:15:32

标签: asp.net-mvc asp.net-identity

我正在使用下面的代码创建LostPassword函数。当用户输入他们的电子邮件时,他们会获得包含要重置的令牌的链接的密码。在我的控制器中,我有GenerateTokenPassword方法(在WebSecurity下),但它抛出了这个错误:

错误:无法将类型'string'转换为'int' 行:var token = WebSecurity.GeneratePasswordResetToken((foundUserName).ToString(),1440);

当我调试时,我在foundUserName字段中获取了用户名,它是一个字符串。 1440是令牌到期时的超时,我提供了一个int来开始。它看起来是什么int?

任何想法可能是什么?

谢谢,

Sanjeev

// POST: Account/LostPassword
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult LostPassword(LostPasswordModel model)
{
    ApplicationUser user;
    using (var db = new MyDbContext())
    {
    var foundUserName = (db.People.Where(p => p.Email == model.Email).FirstOrDefault().UserName);
    user = UserManager.FindByName(foundUserName.ToString());
    //    user = Membership.GetUser(foundUserName);

    //Generate password token that will be used in the email link to authenticate user
    WebSecurity.InitializeDatabaseConnection("", "System.Data.SqlClient", "AspNetUsers", "Id", "UserName", false);
    var token = WebSecurity.GeneratePasswordResetToken((foundUserName).ToString(), 1440);
    // Generate the html link sent via email
    var code = UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

    string resetLink = "<a href='"
        + Url.Action("ResetPassword", "Account", new { rt = token }, "http")
        + "'>Reset Password Link</a>";

    //Email stuff
    string emailsubject = "Reset your account password.";
    string emailbody = "Please click this link to reset your account  password: " + resetLink;
    string emailfrom = "mymail@mymail.edu";
    string emailto = model.Email;


    MailMessage message = new MailMessage();
    message.To.Add(emailto);
    message.From = new MailAddress(emailfrom);
    message.Subject = emailsubject;
    message.Body = emailbody;
    message.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new System.Net.NetworkCredential("mymail", "mypass");
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.Send(message);

    return View();
    }
}

1 个答案:

答案 0 :(得分:0)

出现编译器将1440值解释为字符串,您可以尝试为它声明一个显式int变量并传递它。

或者,给定tokenExpirationInMinutesFromNow是一个可选参数,默认为1440,然后您可以完全省略它

var token = WebSecurity.GeneratePasswordResetToken((foundUserName).ToString());