类型';System.NullReferenceException';发生在未知模块-mvc自定义身份验证中

本文关键字:-mvc 模块 自定义 未知 身份验证 System NullReferenceException 类型 | 更新日期: 2023-09-27 18:20:21

我一直在搜索并试图解决我现在遇到的问题。我有一个用户模型,我想使用自定义身份验证来验证用户。我在提交表单时没有看到任何错误。但我可以在输出窗口中看到异常。这是我的模型类:

public  class UserInfo
{
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int ID { get; set; }
  [Required(ErrorMessage="User Name is required")]
  [Display(Name="User Name")]
  [MaxLength(20, ErrorMessage = "The Maximum length allowed is 20 characters")]
  [MinLength(4, ErrorMessage = "The Minimum length is 3 characters")]
  public string Name { get; set; }
  [Required(ErrorMessage = "Email is required")]
  [EmailAddress(ErrorMessage = "Enter a proper email address")]
  [MaxLength(30, ErrorMessage = "Maximum length allowed for an email is 30 characters")]
  public string Email { get; set; }
  [Required(ErrorMessage = "Password is required")]
  [Display(Name = "Password")]
  [DataType(DataType.Password)]
  public string Pass { get; set; }
  [Required(ErrorMessage = "Confirm your password")]
  [Display(Name = "Confirm Password")]
  [Compare("Pass", ErrorMessage = "Passwords should match")]
  [DataType(DataType.Password)]
  [NotMapped]
  public string Confirm { get; set; }
}

以下是我在控制器中登录的操作方法:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(UserInfo user)
        {
            if (ModelState.IsValid)
            {
                    string EncryptPass;
                    EncryptPass = Crypto.SHA256(user.Pass);
                    Console.WriteLine(EncryptPass);
                    var i = db.Database.ExecuteSqlCommand("select count(*)
                   from userinfos where username = {0}", user.Name);
                    if (i > 0)
                    {
                        var j = db.Database.ExecuteSqlCommand("select 
                count(*) from userinfos where pass = {0}", EncryptPass);
                        if (j > 0)
                        {
                            Session["User"] = user.Name;
                            FormsAuthentication.SetAuthCookie(user.Name,
                             false);
                            RedirectToAction("Create", "Ministry");
                        }
                        else
                        {
                            RedirectToAction("Login");
                            ModelState.AddModelError(user.Pass, "Password is 
                            incorrect");
                        }
                    }
                    else
                    {
                        RedirectToAction("Login");
                        ModelState.AddModelError(user.Name, "Our records 
                          shows
                               that no account exists on your name");
                    }
            }
            return View();
        }

我的观点是:

@model ChurchWebsite.Models.UserInfo
@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Login</h2>
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend></legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>       
        <div class="editor-label">
            @Html.LabelFor(model => model.Pass)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Pass)
            @Html.ValidationMessageFor(model => model.Pass)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

类型';System.NullReferenceException';发生在未知模块-mvc自定义身份验证中

感谢@Stephen为帮助我所做的不懈努力。我创建了一个具有用户名和密码属性的模型

 public  class User
    {
        [Required(ErrorMessage = "User Name is required")]
        [Display(Name = "User Name")]
        [MaxLength(20, ErrorMessage = "The Maximum length allowed is 20 
        characters")]
        [MinLength(4, ErrorMessage = "The Minimum length is 3 characters")]
        public string Name { get; set; }
        [Required(ErrorMessage = "Password is required")]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string Pass { get; set; }
    }

我为型号"用户"添加了控制器

public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(User l,string ReturnUrl="")
        {
                string EncodedPass;
                EncodedPass = Crypto.SHA256(l.Pass);
                var user = db.UserInfo.Where(a => a.Name.Equals(l.Name) && 
                 a.Pass.Equals(EncodedPass)).FirstOrDefault();
                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(l.Name, false);
                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return Redirect(ReturnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Create", "Ministry");
                    }
                }
            ModelState.Remove("Password");
            return View();
        }

我已经为上面的操作方法添加了视图,现在它正在运行良好的