C# 忘记了aspnet标识中的密码
我一直在关注这个 这篇文章似乎不完整 这是我创建的web API,用于根据文章生成忘记密码链接C# 忘记了aspnet标识中的密码,c#,asp.net-web-api,asp.net-identity,C#,Asp.net Web Api,Asp.net Identity,我一直在关注这个 这篇文章似乎不完整 这是我创建的web API,用于根据文章生成忘记密码链接 public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (
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");
}
var 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 here: <a href=\"" + callbackUrl + "\">link</a>");
return View("ForgotPasswordConfirmation");
}
// If we got this far, something failed, redisplay form
return View(model);
}
公共异步任务);
返回视图(“放弃密码确认”);
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
我们可以看到,ResetPassword是在收件箱中单击链接时应该调用的URL操作。但是,本文没有给出ResetPassword API的方法定义。控制器中必须有ResetPassword方法作为操作方法
//
// GET: /Account/ResetPassword
[AllowAnonymous]
public ActionResult ResetPassword(string code)
{
return code == null ? View("Error") : View();
}
//
// POST: /Account/ResetPassword
[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();
}
//
//获取:/Account/ResetPassword
[异名]
公共操作结果重置密码(字符串代码)
{
返回代码==null?视图(“错误”):视图();
}
//
//POST:/Account/ResetPassword
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务控制器中必须有ResetPassword方法作为操作方法
//
// GET: /Account/ResetPassword
[AllowAnonymous]
public ActionResult ResetPassword(string code)
{
return code == null ? View("Error") : View();
}
//
// POST: /Account/ResetPassword
[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();
}
//
//获取:/Account/ResetPassword
[异名]
公共操作结果重置密码(字符串代码)
{
返回代码==null?视图(“错误”):视图();
}
//
//POST:/Account/ResetPassword
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务步骤1)创建一个新的ASP.NET项目,然后选择“身份验证”>“本地用户”。步骤2)将您想要的代码复制到您的项目中。DoneCamilo这可能确实有帮助。但是internet上没有资源会讨论这个问题,呃?不确定,但代码是自动生成代码的复制粘贴。另外,如果您e刚开始,你应该从ASP.NET Core开始,ASP.NET MVC 5不会持续很长时间步骤1)创建一个新的ASP.NET项目,然后选择“身份验证”>“本地用户”。步骤2)将你想要的代码复制到你的项目中。DoneCamilo这可能确实有帮助。但是internet上没有任何资源可以讨论这个问题,嗯?不确定,但是code是自动生成代码的复制粘贴。另外,如果你刚刚开始,你应该从ASP.NET Core开始,ASP.NET MVC 5不会持续很久。这看起来很有希望。我会在一点上实现这个想法。这看起来很有希望。我会在一点上实现这个想法