c# - 控制器中的电子邮件链接
问题描述
我在注册系统后执行确认,代码运行正常,有一个小问题。问题是用户必须点击链接,但这就是它在下图中的显示方式。
图片邮箱:
我实际上不知道我在哪里错过了它,因为我希望链接正确显示并且不显示生成的令牌。如果您可以检查第一个“链接”,则它是不可点击的,其次,另一个链接会显示所有内容。
身份配置
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);
}
解决方案
我设法做到了这样。
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 + "");
推荐阅读
- java - 添加依赖项后出现 UnsatisfiedDependencyException
- react-native - 如何在反应原生的屏幕截图中更改应用程序背景颜色(红色标记)?
- php - 如何在php codeigniter 3中将表单数据添加到动态字符串
- python - Pyspark:pandas_udf、grouped_agg 的多个参数
- php - 为什么会出现 stdClass 而不是我们的自定义对象名称?
- swift - 在swift 4中登录网站的最简单方法是什么
- ssis - 使用 SSIS 在 DB2 表中设置时间戳列
- scala - 在 spark-scala 中将 RDD 数据分成多行
- r - 无法让 devtools::install_github() 在 Win 7 64 位机器上的 R 中工作
- python-3.x - jupyter notebook 没有导入beautifulsoup4?