Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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
C# 在asp中使用表单身份验证对用户进行身份验证_C#_Asp.net_Visual Studio 2013_Forms Authentication - Fatal编程技术网

C# 在asp中使用表单身份验证对用户进行身份验证

C# 在asp中使用表单身份验证对用户进行身份验证,c#,asp.net,visual-studio-2013,forms-authentication,C#,Asp.net,Visual Studio 2013,Forms Authentication,我正在使用mvc,在我的控制器中,我有以下代码: [HttpPost] public ActionResult LogOn(LoginViewModel model, string password) { if (ModelState.IsValid) { if (new BAUser().GetUserbyUsername(model.UserName) != null) {

我正在使用mvc,在我的控制器中,我有以下代码:

 [HttpPost]
    public ActionResult LogOn(LoginViewModel model, string password)
    {
        if (ModelState.IsValid)
        {
            if (new BAUser().GetUserbyUsername(model.UserName) != null)
            {
                UserTable u = new UserTable();
                u = new BAUser().GetUserbyUsername(model.UserName);

                if (u.Password == model.Password)
                {
                    FormsAuthentication.Authenticate(u.Username, u.Password);
                    FormsAuthentication.SetAuthCookie(u.Username, false);
                    Session["LoggedUser"] = u;
                    **if(Request.IsAuthenticated == true)
                    {
                        UserTable user = u;
                    }**
                    return RedirectToAction("Index", "Home");                    }

            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
带有**的if语句只是为了使用断点测试请求是否经过身份验证。但是,身份验证过程似乎不起作用。我做错什么了吗?我正在使用VS2013


我找到了一个使用会话的解决方案,但是如果有人找到了解决方案,请告诉我。

这样的输出是什么:FormsAuthentication.Authenticate(u.Username,u.Password);这将只在身份验证过程中存储用户名和密码,通常情况下,如果(request.Isauthenticated)它应该看到这些值,而不是null,并且返回true而不是false,则在执行此操作时。我从VS2010切换到2013,这部分给我带来了问题。没有身份验证方法对用户进行身份验证并返回布尔值。您关心获取null,因为您没有正确地进行身份验证。在FormAuthentication中设置一个断点。验证(u,p)并检查结果,看看返回了什么。由于某种原因,它似乎在VS 2010上工作,但在2013年给我带来了问题