我使用下面的代码创建一个LostPassword函数。当用户输入他们的电子邮件时,他们会得到一个包含重置令牌链接的密码。在我的控制器中,我有GenerateTokenPassword方法(在WebSecurity下),但它抛出了这个错误:
错误:无法将类型"string"转换为"int"行:var token = WebSecurity.GeneratePasswordResetToken((foundUserName).ToString(), 1440);
当我调试时,我在foundUserName字段中得到我的用户名,它是一个字符串。1440是令牌过期时的超时,我将提供一个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();
}
}
它会出现编译器将1440
值解释为字符串,您可以尝试为它声明一个显式的int
变量并传递它。
或者,假设tokenExpirationInMinutesFromNow
是一个可选参数,默认已经是1440
,那么您可以完全省略它
var token = WebSecurity.GeneratePasswordResetToken((foundUserName).ToString());