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

本文关键字:用户 链接 mvc | 更新日期: 2023-09-27 18:02:53

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

我现在拥有的代码只是向我发送一条短信,其中包含用户的完整url和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);
    }

这在上有效

这是我的短信重置的代码

   // 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是用于重置密码的im。

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

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

SMS消息不是HTML,因此它们不理解锚标记。然而,大多数设备都会让看起来像URL的东西可以点击,但这取决于你要发送到的设备。此外,URL.Action会生成一个相对URL,因为它不在页面上,你需要创建一个完全限定的URL。

sms =  "Please reset your password by clicking " + callbackUrl;
<a href="sms:recepient Phone #;?&body=My Name is- %20Kamlesh%20Gathe">
     Click here to text me
/a>

%20用于提供空间。

此链接可以嵌入任何位置的电子邮件中。