3

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.

Punya Munasinghe
  • 261
  • 1
  • 6
  • 20
  • See https://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity(v=vs.111).aspx. It exists in namespace WebMatrix.WebData and you must add a reference to WebMatrix.WebData.dll – Igor Oct 22 '16 at 12:15
  • This is effectively the same problem as http://stackoverflow.com/q/1274852/1658906. Very simple reference missing problem, either add it manually or install a NuGet package that adds it. – juunas Oct 22 '16 at 12:21

1 Answers1

3

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

After that add using to your class.

using WebMatrix.WebData;
mybirthname
  • 17,949
  • 3
  • 31
  • 55