Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 为什么ModelState在不记录任何错误时无效?_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net 为什么ModelState在不记录任何错误时无效?

Asp.net 为什么ModelState在不记录任何错误时无效?,asp.net,asp.net-mvc,asp.net-mvc-4,Asp.net,Asp.net Mvc,Asp.net Mvc 4,验证登录功能的ModelState时遇到一些问题 设置如下所示: 我有一个数据库表,其中包含大约6个属性,用于记录用户信息。(Id、用户名、电子邮件、密码、CreatedOn、DisabledUser)。我正在使用LocalDb特性和实体框架 为了与数据库进行交互,我使用了Repository模式。(IUserRepository用于定义我的操作和UserRepository,以实现我向Ninject注册为DI容器的具体实现) 用户实体类的定义如下: public class User

验证登录功能的ModelState时遇到一些问题

设置如下所示: 我有一个数据库表,其中包含大约6个属性,用于记录用户信息。(Id、用户名、电子邮件、密码、CreatedOn、DisabledUser)。我正在使用LocalDb特性和实体框架

为了与数据库进行交互,我使用了Repository模式。(IUserRepository用于定义我的操作和UserRepository,以实现我向Ninject注册为DI容器的具体实现)

用户实体类的定义如下:

public class User
    {
        public Int32 Id { get; set; }
        public String Username { get; set; }
        public String Email { get; set; }
        public String Password { get; set; }
        public DateTime CreatedOn { get; set; }
        public Boolean DisabledUser { get; set; }
    }
public class FormsAuthProvider: IAuthProvider
    {
        public bool Authenticate(string username, string password)
        {
            bool result = FormsAuthentication.Authenticate(username, password);
            if (result)
            {
                FormsAuthentication.SetAuthCookie(username, false);
            }

            return result;
        }

    }
为了定义登录过程,我有一个接口的具体实现,定义如下:

public class User
    {
        public Int32 Id { get; set; }
        public String Username { get; set; }
        public String Email { get; set; }
        public String Password { get; set; }
        public DateTime CreatedOn { get; set; }
        public Boolean DisabledUser { get; set; }
    }
public class FormsAuthProvider: IAuthProvider
    {
        public bool Authenticate(string username, string password)
        {
            bool result = FormsAuthentication.Authenticate(username, password);
            if (result)
            {
                FormsAuthentication.SetAuthCookie(username, false);
            }

            return result;
        }

    }
接下来,我用来将数据从模型传递到视图的视图模型是

public class LoginViewModel
    {
        [Required]
        public String Username { get; set; }

        [Required]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
处理登录请求的控制器操作是:

[HttpPost]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (authProvider.Authenticate(model.Username, model.Password))
                {
                    return Redirect(returnUrl ?? Url.Action("Index", "Account"));
                }
                else
                {
                    ModelState.AddModelError("", "User or password was incorrect");
                    return View();
                }      

            }
            else
            {
                return View();
            }
        }
景色

@model pRom.WebUI.Models.LoginViewModel

@{
    ViewBag.Title = "Log In";
    Layout = "~/Views/Shared/_AccountLayout.cshtml";
}

<h2>Login</h2>

<p>Please log in to access the administrative area:
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        @Html.EditorForModel()
        <p><input type="submit" value="Log in" /></p> 
    }
</p>

因此,模型验证工作正常,而身份验证工作不正常。我已经检查了局部变量,传递用于比较的字符串是数据库中的字段。

您使用的是
格式的身份验证。身份验证(已过时),实际上,这是最可能的问题


如果试图利用成员资格API,则需要将
FormsAuthentication.Authentication
替换为
成员资格.ValidateUser
(采用相同的参数)。这要求您在web.config中设置提供程序。否则,您可能需要用您自己的自定义服务类替换Authenticate。

ModelState.IsValid
始终为false,还是您只是没有得到预期的重定向?是,ModelState始终为false。即使有合适的证书或表格上的胡言乱语。我在想,模型绑定没有正确完成,或者有什么问题……你犯了什么错误?Modelstate是否无效,或者Authenticate是否返回false或其他内容?您可以将登录视图添加到问题中吗?如果我正确阅读注释,Modelstate实际上是有效的,否则,您甚至不会点击
Authenticate
。然后,您将添加一个关于
身份验证失败的错误(这就是它失败的地方,不是在模型中,而是在
身份验证中)。嗯。。。