【问题标题】:How can I use the Reset Password Functionality in Web API 2 [closed]如何在 Web API 2 中使用重置密码功能 [关闭]
【发布时间】:2016-12-29 15:12:11
【问题描述】:

我创建了一个方法,当用户请求重置密码时,它会向用户发送一封电子邮件。我现在想生成一个从要打开的网站打开 ResetPassword.cshtml 的 url。我无法访问网站,因为网站页面要求提供我没有的 cookie。

【问题讨论】:

    标签: c# cookies asp.net-web-api2 reset-password


    【解决方案1】:

    首先生成一个重置密码页面的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>
    }
    

    【讨论】:

    • 嗨,Volen,我的 Web API 如何使用正在处理 Cookie 的 [ValidateAntiForgeryToken]。
    • 要使用它,您必须将 MVC 库 (System.Web.MVC.dll) 添加到您的项目中。它应该对你有所帮助。
    • 如何指向 URL 以打开网站上的重置网页。
    • 为此,您可以在我的回答中使用第一个示例中的代码。
    • 尝试使用这个 Url.Link("Default", new { Controller = "Account", Action = "ResetPassword", code = code });避免使用硬编码的 url "localhost:59606/Account/ResetPassword?code="; + 代码;
    猜你喜欢
    • 2012-08-11
    • 2019-06-04
    • 1970-01-01
    • 2020-07-05
    • 2021-09-22
    • 2014-05-31
    • 2013-05-17
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多