2

如何通过页面设置保留姓名、姓氏等信息的全局用户数据?如果我使用会话变量,它会在 auth cookie 之前过期

谢谢!

4

3 回答 3

4

您可以使用身份验证 cookie 中的 userdata 字段存储身份验证会话期间的数据。

下面的代码是默认 MVC 项目中 AccountController 的 LogOn 操作:

 [HttpPost]
 public ActionResult LogOn(LogOnModel model, string returnUrl)
 {
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                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(model.UserName, model.RememberMe);

和:

string fullName = "User full name";

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, model.Email, DateTime.Now,
                DateTime.Now.AddDays(2), model.RememberMe, fullName);

string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) { Expires = DateTime.Now.AddDays(2) });

如果你想存储比普通字符串更多的数据,你将不得不研究某种数据序列化器。

然后,当使用 auth cookie 时,您必须实现一些东西来解析序列化数据。

数据可通过以下方式获得:

((FormsIdentity)User.Identity).Ticket.UserData;

希望有帮助

编辑: 还将 DateTime.Now.AddDays(2) 更改为您希望身份验证会话保持有效的任何内容。

于 2011-05-08T02:58:32.500 回答
1

会话状态可以通过多种方式配置以解决上述问题。具体来说,如果使用进程内会话状态(其中状态保持在与运行应用程序相同的应用程序域/池中),只需将超时时间增加到等于或大于表单 cookie 超时时间。如果您希望将会话存储在进程外(在远程系统或数据库中),请进行相应配置。
这里有一些资源:

于 2011-05-08T02:27:55.883 回答
0

将其存储在您的身份验证 cookie 中可能会被伪造,也可能会过期。怎么样 - 如果你可以让你的 session 和 auth cookie 同时过期,那会有效吗?如果是这样,我可以轻松地为您提供该解决方案。下面的代码检查会话是否不可用,如果是,您将被重定向到登录页面。将下面的 login.aspx 更改为您的登录名。同样在登录时,您必须将 Session["UniqueUserId"] 设置为某个值(可以是任何值.. 只需要设置为某个值)

protected void Application_PreRequestHandlerExecute(对象发送者,EventArgs e)
        {
            //只有在可用时才访问会话状态
            if (Context.Handler 是 IRequiresSessionState || Context.Handler 是 IReadOnlySessionState)
            {
                //如果我们通过了身份验证并且我们在这里没有会话.. 重定向到登录页面。
                HttpCookie authenticationCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authenticationCookie != null)
                {
                    FormsAuthenticationTicket authenticationTicket = FormsAuthentication.Decrypt(authenticationCookie.Value);
                    if (!authenticationTicket.Expired)
                    {
                        if (Session["UniqueUserId"] == null)
                        {
                            //这意味着由于某种原因会话在身份验证票证之前过期。强制登录。
                            FormsAuthentication.SignOut();
                            Response.Redirect("Login.aspx", true);
                            返回;
                        }
                    }
                }
            }
        }

于 2011-05-08T03:53:12.550 回答