【问题标题】:FormsAuthentication and IsValid method does not exist in current context error当前上下文错误中不存在 FormsAuthentication 和 IsValid 方法
【发布时间】:2023-03-08 04:54:01
【问题描述】:

我是 ASP.net Web 应用程序的新手,我目前正在通过从特定数据库获取数据来创建登录验证表单。我在登录过程中使用 FormsAuthentication 模型来绑定目录。但相反,我在模型上收到了红线,如下所示: Error

login.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RedoAssignment.Models;
using Microsoft.EntityFrameworkCore;
using RedoAssignment.Table;
using System.Web;

namespace RedoAssignment.Pages
{
public class loginModel : PageModel
{
    public ActionResult Index()
    {
        return Page();
    }

    [HttpGet]
    public ActionResult login()
    {
        return Page();
    }

    [HttpPost]
    public ActionResult login(Models.RedoAssignmentContext context)
    {
        if (ModelState.IsValid)
            {
               FormsAuthentication.SetAuthCookie(User.email, User.password);
               return RedirectToAction("Index", "Home");
             }

            else
            {
                ModelState.AddModelError("", "Login data is incorrect!");
            }

        return Page();
    }
}

User.cs 模型

 public class User
{
    [Key]
    public int customerID { get; set; }

    [StringLength(60, MinimumLength = 3)]
    [Required]
    [Display(Name = "Full Name")]
    public string fullName { get; set; }

    [Required]
    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    public string email { get; set; }

    [Required]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string password { get; set; }

    [StringLength(20, MinimumLength = 3)]
    [Required]
    [Display(Name = "Contact Number")]
    public string contactNumber { get; set; }

    [StringLength(60, MinimumLength = 3)]
    [Required]
    [Display(Name = "Address")]
    public string address { get; set; }
}

我想知道我哪里出错了,谢谢。

编辑: 我以前添加了 if(User.IsValid(User.email, User.password)) 值,但是 IsValid 语句上有红线。

if (ModelState.IsValid)
        {
          if(User.IsValid(User.email, User.password))
           {
           FormsAuthentication.SetAuthCookie(User.email, User.password);
           return RedirectToAction("Index", "Home");
           }
         }

【问题讨论】:

    标签: c# asp.net razor-pages


    【解决方案1】:
    1. FormsAuthentication.SetAuthCookie() 不接受两个字符串参数作为 参数。
    2. FormsAuthentication.SetAuthCookie(string email, bool createPersistentCookie);
    3. 切勿在 cookie 中设置密码
    4. 您不能使用任何用户模型属性,因为它没有被实例化并且它是 不是静态类。 //用户邮箱
    5. User.IsValid() 将不起作用,因为您的 User 类中没有这样的方法。
    6. 最好使用 .net Identity 或任何其他首选方式来验证用户。
    if(ModelState.IsValid)
    {
     if(condition) //validate whether user exists
      FormsAuthentication.SetAuthCookie(email, true/false);
     else
      ModelState.AddModelError("", "Login data is incorrect!");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多