我已经构建了一个应用程序,并希望在注册后向用户发送确认电子邮件。我已经通过guid = new.guid()获取了ActivationCode值;但是它没有存储在数据库中,并且在调试之后,我评估了它的值:名称“ ActivationCode”在当前上下文中不存在。
public ActionResult Activation()
{
ViewBag.Message = "Invalid Activation code.";//This get outputted always
if (RouteData.Values["id"] != null)
{
Guid activationcode = new Guid(RouteData.Values["id"].ToString());
IPHISEntities usersEntities = new IPHISEntities();
User userActivation = usersEntities.Users.Where(p => p.ActivationCode == activationcode).FirstOrDefault();
if (userActivation != null)//this condition gets true
{
usersEntities.Users.Remove(userActivation);
try {
usersEntities.SaveChanges();
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
}
ViewBag.Message = "Activation successful.";
}
}
return View();
}
private void SendActivationEmail(User user)
{
Guid activationcode = Guid.NewGuid();
IPHISEntities usersEntities = new IPHISEntities();
usersEntities.Users.Add(new User
{
UserId = user.UserId,
ActivationCode = activationcode
});
try
{
usersEntities.SaveChanges();
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
}
using (MailMessage mm = new MailMessage("xyzz@gmail.com", user.EmailAddress))
{
mm.Subject = "Account Activation";
string body = "Hello " + user.FirstName + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + string.Format("{0}://{1}/User/Activation/{2}", Request.Url.Scheme, Request.Url.Authority, activationcode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("xyzz@gmail.com", "********");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
//**User.cs**:
namespace ConnectionSQL_webAPI_.Models
{
using System;
using System.Collections.Generic;
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.Answers = new HashSet<Answer>();
this.Appointments = new HashSet<Appointment>();
this.DoctorInfoes = new HashSet<DoctorInfo>();
this.Payments = new HashSet<Payment>();
this.UserLoginDetails = new HashSet<UserLoginDetail>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public System.Guid ActivationCode { get; set; }
public string Password { get; set; }
public string PhoneNumber { get; set; }
public string UserUniqueId { get; set; }
public Nullable<int> UserTypeId { get; set; }
public Nullable<int> AddressId { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
}
预期产量
激活成功
实际输出
无效的激活码
答案 0 :(得分:0)
将ActivationCode放在不同的表中,并使其在数据库中成为uniqueidentifier。现在在模型中更新数据库,并检查是否存在Guid的ActivationCode属性,然后执行上面的代码。 结果输出:激活成功
答案 1 :(得分:-1)
添加
using System.Runtime.Remoting;