Thari Thari - 2 months ago 14
C# Question

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

I'm using 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.

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 == resetpasswordmodel.Email

if (foundemail != null)
member = Membership.GetUser(foundemail.ToString());
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";
string body = "You link: " + resetLink;
string from = "";
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
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.


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

After that add using to your class.

using WebMatrix.WebData;