Я использовал Asp.Net Identity и реализовал сброс пароля, я посылаю ссылку на электронную почту пользователя для сброса пароля.
Вот код:
[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();
}
Вот webApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new
System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
Вот ссылка, отправленная на электронную почту: (посмотрите в ее формате)!
>
http://localhost:7524/api/Account/ManageAccount/reset-password?userId=1011&code=vbGi%2FzN0oFjw6RLlFVuBHiyEz2rH%2FNaO7tc5Y7Y47vzKKC5aNgx9yzZLbHtMD1%2BVZYCot1dvRZSLupPUYcxpCW%2FIl4cJwAIxVjVYA1kxrIjobdrXVqHNMXJmTF5u6cc%2FJdA0uDlQzNjoG4%2Fcjfl3ToRxarZokxI3VN8TEvt1I2M%3D
и я получил:
Неверный URI: невозможно определить формат URI.
Что я делаю не так?