MVC 4 SimpleMembership - 为什么WebSecurity.CurrentUserId -1登录后 [英] MVC 4 SimpleMembership - Why WebSecurity.CurrentUserId -1 after login

查看:167
本文介绍了MVC 4 SimpleMembership - 为什么WebSecurity.CurrentUserId -1登录后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置登录时的cookie,并具有登录后获取当前用户ID的问题。在下面的,intUserId为-1,WebSecurity.IsAuthenticated是假的。这难道不是把这个code正确的地方吗?在此之后,它重定向到主页...所以不知道为什么这是不正确的地方。

  // POST:/帐号/登录        [HttpPost]
        [使用AllowAnonymous]
        [ValidateAntiForgeryToken]
        公众的ActionResult登录(LoginModel型号,串RETURNURL)
        {
            如果(ModelState.IsValid&安培;&安培; WebSecurity.Login(model.UserName,model.Password,persistCookie:model.RememberMe))
            {
                // TODO:设置当前公司设备基于本用户可以访问
                INT intUserId = WebSecurity.GetUserId(User.Identity.Name);
                INT intUserId2 = WebSecurity.GetUserId(model.UserName);
                UserSession preferences.CurrentCompanyId = 1;
                UserSession preferences.CurrentFacilityId = 1;                返回RedirectToLocal(RETURNURL);
            }            //如果我们走到这一步,事情失败了,重新显示形式
            ModelState.AddModelError(,提供的用户名或密码不正确。);
            返回查看(模型);
        }


解决方案

登录仅设置窗体身份验证cookie。

方式asp.net验证工作原理是,它必须读取cookie设置身份验证请求,但因为当登录页面启动该cookie不存在,该框架不知道用户什么。

重新加载页面,你会发现信息是可用的。

仅供参考,这是新鲜事与SimpleMembership或WebSecurity,这是方法窗体身份验证始终奏效。

I am trying to set a cookie upon login and having issues with getting the current user id after login. In the below, intUserId is -1 and WebSecurity.IsAuthenticated is false. Is this not the correct place to put this code? After this, it redirects to the home page...so not sure why this is not the correct place.

// 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);
        }

解决方案

Login only sets the Forms Authentication cookie.

The way asp.net authentication works is that it must read the cookie to set the authenticate the request, but since the cookie did not exist when the Login page was started, the framework doesn't know anything about the user.

Reload the page, and you will find the information is available.

FYI, this is nothing new with SimpleMembership or WebSecurity, this is the way Forms Authentication has always worked.

这篇关于MVC 4 SimpleMembership - 为什么WebSecurity.CurrentUserId -1登录后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆