This is my model class:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "EMail")]
public string Email { get; set; }
}
//User profile
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
}
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password,new { Email = model.Email }, true);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
return View(model);
}
public ActionResult ForgotPassword(string un, string rt)
{
UsersContext db = new UsersContext();
var userid = (from i in db.UserProfiles
where i.UserName == un
select i.UserId).FirstOrDefault();
bool any = (from j in db.webpages_Memberships
where (j.UserId == userid)
&& (j.PasswordVerificationToken == rt)
select j).Any();
if (any == true)
{
string newpassword = GenerateRandomPassword(6);
bool response = WebSecurity.ResetPassword(rt, newpassword);
if (response == true)
{
var emailid = (from i in db.UserProfiles
where i.UserName == un
select i.Email).FirstOrDefault();
string subject = "New Password";
string body = "Please find the New Password<br />" + newpassword;
try
{
SendEMail(emailid, subject, body);
TempData["Message"] = "Mail Sent.";
}
catch (Exception ex)
{
TempData["Message"] = "Error occured while sending email." + ex.Message;
}
TempData["Message"] = "Success! Check email we sent. Your New Password Is " + newpassword;
}
else
{
TempData["Message"] = "Hey, avoid random request on this page.";
}
}
else
{
TempData["Message"] = "Username and token not maching.";
}
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ForgotPassword(string UserName)
{
var user = Membership.GetUser(UserName);
if (user == null)
{
TempData["Message"] = "User Not exist.";
}
else
{
var token = WebSecurity.GeneratePasswordResetToken(UserName);
var resetLink = "<a href="" + Url.Action("ResetPassword", "Account", new { un = UserName, rt = token }, "http") + "">Reset Password</a>";
UsersContext db = new UsersContext();
var emailid = (from i in db.UserProfiles
where i.UserName == UserName
select i.Email).FirstOrDefault();
string subject = "Password Reset Token";
string body = "Please find the Password Reset Token<br />" + resetLink;
try
{
SendEMail(emailid, subject, body);
TempData["Message"] = "Mail Sent.";
}
catch (Exception ex)
{
TempData["Message"] = "Error occured while sending email." + ex.Message;
}
TempData["Message"] = resetLink;
}
return View();
}
Finally i got the error invalid column name how to insert the values for the time of registration pls help.