2016-08-22 74 views
4

我已經創建了一種方法,以便當用戶請求重置密碼時,它會向用戶發送電子郵件。我現在想要生成一個URL,從網站打開ResetPassword.cshtml打開。我無法訪問網站,因爲網站頁面要求提供我沒有的cookies。如何在Web API中使用重置密碼功能2

回答

3

首先產生重置密碼頁面的URL:

string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); 
//For MVC controller 
var callbackUrl = Url.Action("ResetPassword", "Account", new { code = code }, protocol: Request.Url.Scheme); 
//For Web API controller 
var callbackUrl = Url.Link("Default", new { Controller = "Account", Action = "ResetPassword", code = code }); 

後,它創建MVC控制器復位密碼的方法:

 [AllowAnonymous] 
     public ActionResult ResetPassword(string code) 
     { 
      return code == null ? View("Error") : View(); 
     } 

     [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 
      } 
      var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password); 
      if (result.Succeeded) 
      { 
       //... 
      } 
      AddErrors(result); 
      return View(); 
     } 

然後創建模型接受新密碼:

public class ResetPasswordViewModel 
{ 
    [Required] 
    [EmailAddress] 
    [Display(Name = "Email")] 
    public string Email { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    public string Code { get; set; } 
} 

最後創建重置密碼的視圖:

@model TreeTag.Models.ResetPasswordViewModel 
@{ 
    ViewBag.Title = "Reset password"; 
} 

<h2></h2> 

@using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h4>Reset your password.</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    @Html.HiddenFor(model => model.Code) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div id="confirm_password" class="form-group"> 
     @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" class="btn btn-default" value="Reset" /> 
     </div> 
    </div> 
} 
+0

Volen嗨,我的Web API如何使用[ValidateAntiForgeryToken] 這是工作的餅乾.. – cell

+1

要使用它,你必須MVC LIB(System.Web.Mvc.dll程序)添加到項目中。它應該幫助你。 – Volen

+0

如何指定URL以在網站上打開重置網頁。 – cell

相關問題