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

asp.net-mvc – MVC 4 SimpleMembership – 登录后为什么WebSecu

发布时间:2020-12-16 04:32:22 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试在登录时设置cookie,并在登录后获取当前用户ID时遇到问题.在下面,intUserId是-1,WebSecurity.IsAuthenticated是false.这不是放置此代码的正确位置吗?在此之后,它会重定向到主页…所以不确定为什么这不是正确的地方. // POST: /Account/Login [Htt
我正在尝试在登录时设置cookie,并在登录后获取当前用户ID时遇到问题.在下面,intUserId是-1,WebSecurity.IsAuthenticated是false.这不是放置此代码的正确位置吗?在此之后,它会重定向到主页…所以不确定为什么这不是正确的地方.
// POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model,string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName,model.Password,persistCookie: model.RememberMe))
            {
                //TODO: set current company and facility based on those this user has access to
                int intUserId = WebSecurity.GetUserId(User.Identity.Name);
                int intUserId2 = WebSecurity.GetUserId(model.UserName);
                UserSessionPreferences.CurrentCompanyId = 1;
                UserSessionPreferences.CurrentFacilityId = 1;

                return RedirectToLocal(returnUrl);
            }

            // If we got this far,something failed,redisplay form
            ModelState.AddModelError("","The user name or password provided is incorrect.");
            return View(model);
        }

解决方法

仅登录设置表单身份验证cookie.

asp.net身份验证的工作方式是它必须读取cookie以设置对请求进行身份验证,但由于在启动“登录”页面时cookie不存在,因此框架对该用户一无所知.

重新加载页面,您会发现信息可用.

仅供参考,这对于SimpleMembership或WebSecurity来说并不是什么新鲜事,这是表单身份验证始终有效的方式.

(编辑:李大同)

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

    推荐文章
      热点阅读