Identity 2.0用户将被重定向回Login

时间:2015-02-11 21:08:19

标签: c# asp.net asp.net-identity

这是我的登录代码,登录后重定向到“个人资料”的用户会遇到问题。

 protected void UserLogin_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {

                var manager = new IdentityModels.UserManager();

                IdentityModels.User user = manager.Find(Username.Text, Password.Text);

                if (user != null)
                {
                    if (isUserActive(user.PatientId)=="isNotActive")
                    {
                        lblError.Text =
                            "you are no longer active. Please contact your local clinic to find out why.";
                        return;

                    }
                    if (isUserActive(user.PatientId) == "clinicNotActive")
                    {
                        lblError.Text =
                            "Your clinic is no longer active. Please contact your local clinic to find out why.";
                        return;

                    }
                    IdentityModels.IdentityHelper.SignIn(manager, user, RememberMe.Checked);

                    if (manager.IsInRole(user.Id,"Administrator") || manager.IsInRole(user.Id,"Staff") || manager.IsInRole(user.Id,"Physician"))
                    {
                        Response.Redirect("Dashboard");
                    }


                    if (Request.QueryString["Profile"] != null)
                    {
                        IdentityModels.IdentityHelper.RedirectToReturnUrl(Request.QueryString["Profile"], Response);
                    }
                    else
                    {
                        Response.Redirect("Profile");
                    }


                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password");
                    lblError.Text = "Invalid username or password";
                }
            }
        }

这是我在“个人资料”页面上的页面加载代码:

 var manager = new IdentityModels.UserManager();
            IdentityModels.User user = manager.FindById(HttpContext.Current.User.Identity.GetUserId());

        if (user == null)
        {

             var ex = new Exception("patient was null, BUT TRIED SIGNING IN NOW" + UserAccess.GetUserId().ToString());
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

            Response.Redirect("Login");
        }

Elmah日志显示例外情况“患者为空,但现在已经签名0”。

因此,如果我的用户成功登录,他们必须是因为他们正在访问个人资料页面,那么为什么他们中的一些人会遇到此错误。为什么用户为空?

我无法弄清楚,为什么它会影响一些但不是全部。当我重新发布网站时,所有用户都可以登录几分钟,有时几个小时,然后重新开始。

2 个答案:

答案 0 :(得分:1)

尝试使用User.Identity而不是HttpContext.Current.User.Identity。我已经看到一些情况,其中上下文(基于ASP.NET的会话)与Identity的令牌不同步。

答案 1 :(得分:1)

好的伙计们,这就是答案。

将会话状态从InProc更改为SQLServer,自登录重定向以来已经过了22个小时,之前没有发生,所以我认为可以安全地说问题已经解决了,这就是答案。< / p>

相关问题