加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – User.Identity.IsAuthenticated在设置cookie并进

发布时间:2020-12-16 00:28:33 所属栏目:asp.Net 来源:网络整理
导读:我对MVC4用户授权有问题。 System.Web.Security.Membership.ValidateUser返回true。 然后它到达FormsAuthentication.SetAuthCookie,我在浏览器中看到一个cookie。 然后User.Identity.IsAuthenticated由于某些原因仍然评估为false。 User.Identity.IsAuthent
我对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);
}

解决方法

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

见http://msdn.microsoft.com/en-us/library/twk5762b.aspx

The SetAuthCookie method adds a forms-authentication ticket to either the cookies collection,or to the URL if CookiesSupported is false. The forms-authentication ticket supplies forms-authentication information to the next request made by the browser.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读