Thari Thari - 1 month ago 12
C# Question

How to fix 'The name 'WebSecurity' does not exist in the current context'?

I'm using asp.net c# mvc with Entity Framework to create a website. In there I want to create reset password part And I have the following code for the controller.

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ResetPassword(ResetPasswordViewModel resetpasswordmodel)
{
if (ModelState.IsValid)
{
//User user;
MembershipUser member;
using (TheFoodyContext db = new TheFoodyContext())
{
var foundemail = (from e in db.Users
where e.email == resetpasswordmodel.Email
select e.email).FirstOrDefault();

if (foundemail != null)
{
member = Membership.GetUser(foundemail.ToString());
}
else
{
member = null;
}
}

if (member != null)
{
//Generate password token that will be used in the email link to authenticate user
var token = WebSecurity.GeneratePasswordResetToken(member.Email);
// Generate the html link sent via email
string resetLink = "<a href='"
+ Url.Action("ResetPassword", "Account", new { rt = token }, "http")
+ "'>Reset Password Link</a>";
// Email stuff
string subject = "Reset your password for TheFoody.com";
string body = "You link: " + resetLink;
string from = "abcd123@gmail.com";
string to = resetpasswordmodel.Email;

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient();

// Attempt to send the email
try
{
client.Send(message);
}
catch (Exception e)
{
ModelState.AddModelError("", "Issue sending email: " + e.Message);
}
}
else // Email not found
{
ModelState.AddModelError("", "No user found by that email.");
}
}
return View(resetpasswordmodel);
}


Here var token = WebSecurity.GeneratePasswordResetToken(member.Email); code part which is used to generate password token that will be used in the email link to authenticate user has a error for WebSecurity part. It is saying


The name 'WebSecurity' does not exist in the current context.


I have added following also

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TheFoody.Models;
using TheFoody.DataAccess;
using System.Web.Security;
using System.Web.Mail;
using System.Net.Mail;


I don't know how to fix this error.

Answer

You need to add reference WebMatrix.WebData to your project ! WebSecurity Class

After that add using to your class.

using WebMatrix.WebData;
Comments