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

时间:2016-10-22 12:07:37

标签: c#

我正在使用带有Entity Framework的asp.net c#mvc来创建一个网站。在那里我想创建重置密码部分我有控制器的以下代码。

Function Inc(ByRef i As Integer)
   i = i + 1
End Function

此处 var token = WebSecurity.GeneratePasswordResetToken(member.Email); 代码部分,用于生成将在电子邮件链接中用于验证用户的密码令牌,但有错误WebSecurity 部分。这是说

  

当前上下文中不存在名称“WebSecurity”。

我还添加了以下内容

[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);
}

我不知道如何修复此错误。

1 个答案:

答案 0 :(得分:3)

您需要在项目中添加引用WebMatrix.WebData!的 WebSecurity Class

之后添加使用到您的班级。

using WebMatrix.WebData;