首页 > 解决方案 > 控制器中的电子邮件链接

问题描述

我在注册系统后执行确认,代码运行正常,有一个小问题。问题是用户必须点击链接,但这就是它在下图中的显示方式。

图片邮箱:

在此处输入图像描述

我实际上不知道我在哪里错过了它,因为我希望链接正确显示并且不显示生成的令牌。如果您可以检查第一个“链接”,则它是不可点击的,其次,另一个链接会显示所有内容。

身份配置

  void sendMail(IdentityMessage message)
    {
        #region formatter
        string text = string.Format("Please click on this link to {0}: {1}", message.Subject, message.Body);
        string html = "Please confirm your account by clicking this link: <a href=\'" + message.Body + "\'>link</a><br/>";

        html += HttpUtility.HtmlEncode(@"Or click on the copy the following link on the browser:" + message.Body);
        #endregion

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(ConfigurationManager.AppSettings["Email"].ToString());
        msg.To.Add(new MailAddress(message.Destination));
        msg.IsBodyHtml = true;
        msg.Body = message.Body;
        msg.Subject = message.Subject;
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
        msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["Email"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
        smtpClient.Credentials = credentials;
        smtpClient.EnableSsl = true;
        smtpClient.Send(msg);

    }

帐户控制器

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Name = model.Name, Address = model.Address, PhoneNumber = model.PhoneNumber };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                //Assign Role to user Here
                await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);
                //Ends Here 

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);


                // Send an email with this link
                string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");


                if (UserManager.IsInRole(user.Id, "User"))
                {
                    return RedirectToAction("Index", "Shop");
                }

                if (UserManager.IsInRole(user.Id, "Admin"))
                {
                    return RedirectToAction("Index", "Admin");
                }

                //return RedirectToAction("Index", "Shop");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

标签: c#asp.netasp.net-mvcsmtpclient

解决方案


我设法做到了这样。

var callbackUrl= Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code });
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, callbackUrl);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", " " + link + "");

推荐阅读