如何通过mvc中的短信向用户发送可点击链接

时间:2015-07-16 07:24:56

标签: c# asp.net-mvc asp.net-identity-2

您好我试图通过短信向用户发送resetpassword链接,就像您使用密码重置为mvc中的电子邮件重置一样

我现在的代码只是给我发送了一个带有完整网址的短信和用户的ID而不是我点击的部分

这是我的电子邮件代码重置,首先显示我如何重置

   [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
    {
        //SendSmsBusiness objap = new SendSmsBusiness();
        RegisterBusiness reg = new RegisterBusiness();
        EmailBusiness _emailBusiness = new EmailBusiness();
        if (ModelState.IsValid)
        {

            ApplicationUser user = new ApplicationUser();
            user = reg.UserManager.FindByEmail(model.Email);

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            if (user != null)
            {
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol:Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
                _emailBusiness.SendEmailForgot(model.Email, callbackUrl);
                return RedirectToAction("ForgotPasswordConfirmation", "Account");
            }

            {
                ModelState.AddModelError("", "The user does not exist");
                return View();
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

这适用于fyn

这是我的短信重置代码

   // GET: /Account/ForgotPassword
    [AllowAnonymous]
    public ActionResult PhoneReset()
    {
        return View();
    }
    // POST: /Account/ForgotPassword
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> PhoneReset(ForgotPasswordView model,string sms)
    {
        RegisterBusiness reg = new RegisterBusiness();

        if (ModelState.IsValid)
        {
            ApplicationUser user = new ApplicationUser();
            user = reg.UserManager.FindByEmail(model.Email);
            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            if (user != null)
            {
                //string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                SendSmsBusiness objap = new SendSmsBusiness();

                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                sms =  "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>";
                objap.Send_SMS(model.Phone, sms);
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("ResetViaPhone", "Account");
            }
            else if (user == null)
            {
                ModelState.AddModelError("", "The user does not exist");
                return View();
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }

在短信中我得到了这个 - 请点击重置密码

但没有链接。 minenhle@gmail.com是我用来重置密码的。

我不知道我哪里弄错了或者我尝试的是不可能的

2 个答案:

答案 0 :(得分:1)

短信不是HTML,因此他们不了解锚标记。但是,大多数设备会使看起来像URL的东西可以点击,但这取决于您要发送到的设备。此外,Url.Action会生成相对网址,因为它不在网页上,您需要创建一个完全限定的网址。

sms =  "Please reset your password by clicking " + callbackUrl;

答案 1 :(得分:0)

<a href="sms:recepient Phone #;?&body=My Name is- %20Kamlesh%20Gathe">
     Click here to text me
/a>

%20用于提供空间。

此链接可以嵌入到任何地方的电子邮件中。

相关问题