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 mvc User.Identity.IsAuthenticated在设置cookie并验证后返回false_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc User.Identity.IsAuthenticated在设置cookie并验证后返回false

Asp.net mvc User.Identity.IsAuthenticated在设置cookie并验证后返回false,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我在MVC4用户授权方面遇到问题 System.Web.Security.Membership.ValidateUser返回true 然后它进入FormsAuthentication.SetAuthCookie,我在浏览器中看到一个cookie。 由于某种原因,User.Identity.IsAuthenticated仍然计算为false。 User.Identity.IsAuthenticated在重定向后仍然为false,并保持为false [AllowAnonymous] [HttpPos

我在MVC4用户授权方面遇到问题

System.Web.Security.Membership.ValidateUser
返回
true

然后它进入
FormsAuthentication.SetAuthCookie
,我在浏览器中看到一个cookie。
由于某种原因,
User.Identity.IsAuthenticated
仍然计算为
false

User.Identity.IsAuthenticated
在重定向后仍然为false,并保持为
false

[AllowAnonymous]
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (System.Web.Security.Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                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);
}

User.Identity.IsAuthenticated
在调用
FormsAuthentication.SetAuthCookie()
后的下一个请求之前不会设置为true

SetAuthCookie方法将窗体身份验证票证添加到 cookies集合或URL(如果CookieSupported为false)。 表单身份验证票证提供表单身份验证 有关浏览器下一个请求的信息


它需要一个额外的往返(重定向到动作),但这实现了我想要的。此外,我在本示例中没有使用强类型模型,但它演示了这个想法。代码检查用户是否经过身份验证,如果未设置cookie,则重定向到自身。第二次调用IsAuthenticated时为true,并返回视图。只需确保表单输入名为userName和password

[AllowAnonymous]
[HttpPost]
public ActionResult Login(string userName, string password, string returnUrl)
{
if (ModelState.IsValid)
{
    if (HttpContext.User.Identity.IsAuthenticated)
    {
        return View(returnUrl);
    }
    else
    {
        if (System.Web.Security.Membership.ValidateUser(userName, password))
        {
            FormsAuthentication.SetAuthCookie(userName, false);
            if (Url.IsLocalUrl(returnUrl))
            {
                return RedirectToAction("Login", new { userName = userName, password = password, returnUrl = returnUrl });
                //return Redirect(returnUrl);
            }
            else
            {
                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);
}

检查您的网络配置,您必须启用表单身份验证:

在内部添加以下代码段

   <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="3600" />
    </authentication>

如果在您的网络配置中,请将其注释掉:

<!--<modules>
      <remove name="FormsAuthentication" />
</modules>-->

现在你可以检查了


WebSecurity.CurrentUserName、WebSecurity.CurrentUserId和WebSecurity.IsAuthenticated标志

我好不容易才发现这一点。如果在验证后立即进行检查,则在同一代码块中,重定向用户后,no diceUser.Identity.IsAuthenticated仍然为false,并保持为false。在调用
SetAuthCookie
时,您能否验证
model.UserName
是否为null或空?我也有同样的问题。具有web配置设置和正确的代码,但总是false@corywest在你使用的地方张贴代码SetAuthCookie@corywest您的web.config看起来像什么?你在那里指定了表单身份验证吗?@Dismissile身份验证一周前就开始工作了。我不知道我做了什么来打破它。代码块中的代码从那时起就没有改变。你救了我@Nalan MI丢失了删除部分,非常感谢。是的,我只注释了标记,因为Spiew也使用了该部分,但这肯定解决了我的问题!!谢谢