Tôi nhận được: Mã thông báo là mã nhận dạng được chọn tự do, nhận dạng tùy chọn mật khẩu. Ví dụ,
1. trông giống như quá trình khôi phục mật khẩu, bước 1 (nó được dựa trên: https://stackoverflow.com/a/698879/208922)
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
//[RecaptchaControlMvc.CaptchaValidator]
public virtual async Task<ActionResult> ResetPassword(
ResetPasswordViewModel rpvm)
{
string message = null;
//the token is valid for one day
var until = DateTime.Now.AddDays(1);
//We find the user, as the token can not generate the e-mail address,
//but the name should be.
var db = new Context();
var user = db.Users.SingleOrDefault(x=>x.Email == rpvm.Email);
var token = new StringBuilder();
//Prepare a 10-character random text
using (RNGCryptoServiceProvider
rngCsp = new RNGCryptoServiceProvider())
{
var data = new byte[4];
for (int i = 0; i < 10; i++)
{
//filled with an array of random numbers
rngCsp.GetBytes(data);
//this is converted into a character from A to Z
var randomchar = Convert.ToChar(
//produce a random number
//between 0 and 25
BitConverter.ToUInt32(data, 0) % 26
//Convert.ToInt32('A')==65
+ 65
);
token.Append(randomchar);
}
}
//This will be the password change identifier
//that the user will be sent out
var tokenid = token.ToString();
if (null!=user)
{
//Generating a token
var result = await IdentityManager
.Passwords
.GenerateResetPasswordTokenAsync(
tokenid,
user.UserName,
until
);
if (result.Success)
{
//send the email
...
}
}
message =
"We have sent a password reset request if the email is verified.";
return RedirectToAction(
MVC.Account.ResetPasswordWithToken(
token: string.Empty,
message: message
)
);
}
2 Và sau đó khi người dùng nhập vào các dấu hiệu và mật khẩu mới :
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
//[RecaptchaControlMvc.CaptchaValidator]
public virtual async Task<ActionResult> ResetPasswordWithToken(
ResetPasswordWithTokenViewModel
rpwtvm
)
{
if (ModelState.IsValid)
{
string message = null;
//reset the password
var result = await IdentityManager.Passwords.ResetPasswordAsync(
rpwtvm.Token,
rpwtvm.Password
);
if (result.Success)
{
message = "the password has been reset.";
return RedirectToAction(
MVC.Account.ResetPasswordCompleted(message: message)
);
}
else
{
AddErrors(result);
}
}
return View(MVC.Account.ResetPasswordWithToken(rpwtvm));
}
Skeleton proposal để sample project trên github, nếu có ai cần nó có thể tested.The E-mail gửi chưa được viết, có thể với việc bổ sung sớm.
Nguồn
2013-09-26 19:05:38
Cảm ơn sự giúp đỡ và hỗ trợ của bạn! Vấn đề của tôi là phương thức (IdentityManager.Passwords.GenerateResetPasswordToken) được mô tả không thể tìm thấy bất kỳ thông tin nào ở bất cứ đâu, vì vậy tôi không thể sử dụng nó. Nhưng dù sao, tôi sẽ sớm bù đắp cho các thông tin còn thiếu và tôi sẽ bao gồm mã hợp lệ! –
vậy sự khác nhau giữa 'IdentityManager' và' UserManager' là gì?Khi tôi tạo một dự án mới, 'AccountController' đã sử dụng' UserManager'. – Eonasdan
Tôi đã chuyển câu trả lời của bạn cho câu trả lời của bạn, thay vì câu hỏi trong câu hỏi. –