从WebApi调用html页面,无效的URI:无法确定URI的格式

时间:2019-04-11 14:21:49

标签: c# asp.net asp.net-web-api

我使用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();
    }

这是发送到电子邮件的链接:(请看其格式)!

  

>   http://localhost:7524/api/Account/ManageAccount/reset-password?userId=1011&code=vbGi%2FzN0oFjw6RLlFVuBHiyEz2rH%2FNaO7tc5Y7Y47vzKKC5aNgx9yzZLbHtMD1%2BVZYCot1dvRZSLupPUYcxpCW%2FIl4cJwAIxVjVYA1kxrIjobdrXVqHNMXJmTF5u6cc%2FJdA0uDlQzNjoG4%2Fcjfl3ToRxarZokxI3VN8TEvt1I2M%3D

我得到了:
无效的URI:无法确定URI的格式。

我在做什么错了?

0 个答案:

没有答案