微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

控制器中的电子邮件链接

如何解决控制器中的电子邮件链接

我正在实现注册系统后的确认,并且代码运行正常,存在一个小问题。问题是用户必须单击链接,但这是在下面的图片显示的方式。

图片电子邮件

enter image description here

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

IdentityConfig

  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,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);
    }

解决方法

我设法做到了。

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 + "");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。