简体   繁体   English

如何解决'当前上下文中不存在'WebSecurity'这个名字'?

[英]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. 我正在使用asp.net c#mvc和Entity Framework来创建一个网站。 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); 这里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. 用于生成密码令牌的代码部分将在电子邮件链接中用于对用户进行身份验证,但WebSecurity部分出现错误。 It is saying 这是说

The name 'WebSecurity' does not exist in the current context. 当前上下文中不存在名称“WebSecurity”。

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 ! 您需要将参考WebMatrix.WebData添加到您的项目中! WebSecurity Class WebSecurity类

After that add using to your class. 之后添加使用到您的班级。

using WebMatrix.WebData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM