I used Asp.Net Identity and I implemented reset password, I send a link to user Email to reset password.
Here is the code:
[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
{
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 });
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();
}
[AcceptVerbs("GET")]
[AllowAnonymous]
[Route("ManageAccount")]
public IHttpActionResult ManageAccount(string id)
{
if (! String.IsNullOrEmpty(id))
{
string page = id + ".html";
return Redirect(page);
}
return Redirect("Login.html");
}
[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)
{
}
var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
if (result.Succeeded)
{
return Ok();
}
return InternalServerError();
}
Here is webApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new
System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
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);
}
}
What I have tried:
In AccountController I tried this but it doesn't work:
[HttpGet]
[AllowAnonymous]
public async Task<IHttpActionResult> ManageAccount(string id)
{
if (! String.IsNullOrEmpty(id))
{
string page = id + ".html";
return Redirect(page);
}
return Redirect("Login.html");
}
Also,
[AcceptVerbs("GET")]
[AllowAnonymous]
[Route("ManageAccount/{id}")]
public async Task<IHttpActionResult> ManageAccount(string id)
{
if (! String.IsNullOrEmpty(id))
{
string page = id + ".html";
return Redirect(page);
}
return Redirect("Login.html");
}
Here is the link sent to email:(Look at its format) !
http:
When I click on it I got:
Invalid URI: The format of the URI could not be determined.
What I need is call html page "with url that have userId and code authentication" from WebApi
I'm really stuck Could you please help?