我使用Asp.Net Identity并实现了重置密码,我向用户电子邮件发送了一个链接以重置密码。
代码如下:
1[System.Nullable
这是webApiConfig:
[HttpPost]
[Route("ForgotPassword")]
[AllowAnonymous]
public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
return BadRequest("Either user does not exist or you have not confirmed your email.");
}
try
{
// Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
Url.Link("DefaultApi",
new { controller = "Account/ConfirmEmail", userId = user.Id, code = code });
string callbackUrl = Url.Link("DefaultApi",
new { controller = "Account/ManageAccount/reset-password", userId = user.Id, code = code });
//string callbackUrl = Url.Link("Default",
// new { controller = "User/ManageAccount/reset-password", userId = user.Id, code = code });
await UserManager.SendEmailAsync(user.Id, "Reset Password",
"Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
return Ok();
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
return BadRequest();
}
// GET api/Account/ManageAccount
[AcceptVerbs("GET")]
[AllowAnonymous]
[Route("ManageAccount/{id}")]
public IHttpActionResult ManageAccount(string id)
{
if (! String.IsNullOrEmpty(id))
{
string page = id + ".html";
return Redirect(page);
}
return Redirect("Login.html");
}
// POST: /Account/ResetPassword
[HttpPost]
[AllowAnonymous]
[Route("ResetPassword")]
public async Task<IHttpActionResult> ResetPassword(ResetPasswordViewModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null)
{
// return Redirect("https://localhost:44342/Login.html");
}
var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
if (result.Succeeded)
{
return Ok();
}
return InternalServerError();
}
这是发送到电子邮件的链接:(请看其格式)!
我得到了:
无效的URI:无法确定URI的格式。
我在做什么错了?