Session asp.net mvc 3-设置全局用户数据

Session asp.net mvc 3-设置全局用户数据,session,authentication,asp.net-mvc-3,user-data,Session,Authentication,Asp.net Mvc 3,User Data,如何设置全局用户数据,以通过页面保留一些信息,如名称、姓氏等?如果我使用会话变量,它将在验证cookie之前过期 谢谢 会话状态可以通过多种方式解决上述问题。具体地说,如果使用进程内会话状态,其中状态与正在运行的应用程序保持在同一应用程序域/池中,只需将超时增加到等于或大于cookie timeout的形式。如果希望将进程外会话存储在远程系统或数据库中,请进行相应的配置。 以下是一些资源: 您可以使用auth cookie中的userdata字段存储auth会话期间的数据 以下代码是默认MVC项

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

谢谢

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


您可以使用auth cookie中的userdata字段存储auth会话期间的数据

以下代码是默认MVC项目中AccountController的登录操作:

 [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);
与:

如果您想存储的数据超过正常字符串中的数据量,那么您必须研究某种数据序列化程序

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

可通过以下途径获取数据:

((FormsIdentity)User.Identity).Ticket.UserData;
希望有帮助

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

将其存储在身份验证cookie中可以伪造,也可以过期。 这个怎么样?如果您可以使会话和身份验证cookie同时过期,那么这样做行吗?如果是这样,我可以很容易地为您提供解决方案。 下面的代码检查会话是否不可用,如果不可用,您将被重定向到登录页面。 将下面的login.aspx更改为您的登录名。此外,在登录时,您必须将会话[UniqueUserId]设置为某个值,该值可以是任何值。。只是需要做点什么

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) { //Only access session state if it is available if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState) { //If we are authenticated AND we dont have a session here.. redirect to login page. HttpCookie authenticationCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authenticationCookie != null) { FormsAuthenticationTicket authenticationTicket = FormsAuthentication.Decrypt(authenticationCookie.Value); if (!authenticationTicket.Expired) { if (Session["UniqueUserId"] == null) { //This means for some reason the session expired before the authentication ticket. Force a login. FormsAuthentication.SignOut(); Response.Redirect("Login.aspx", true); return; } } } } }
你希望它持续多久?跨会话?是的,跨身份验证会话,我确实使用了会话变量,但越早过期,您需要将其存储到数据库或其他形式的持久存储。好的,但是,如何在所有控制器上获取DB信息而不必在每个控制器上编写代码?重构代码,以便只有一个或几个控制器需要访问此信息,并使用RenderPartial方法等方法使其显示在所有页面上。谢谢,我喜欢这种方法,但我不知道为什么我不能使其工作,我用…配置web.conf。。。。但是我的会话变量仍在过期您是否对应用程序池进行了任何可能导致其回收的更改?此外,某些其他活动会导致应用程序池循环使用,即修改web.config、重建应用程序、修改网站下的文件5次以上。啊,我正在进行项目,因此,重建和发布是经常性的工作!谢谢这并不“可怕”,但在客户端存储数据已经证明不是一个最佳解决方案——特别是在最近的POET漏洞之后。 protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) { //Only access session state if it is available if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState) { //If we are authenticated AND we dont have a session here.. redirect to login page. HttpCookie authenticationCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authenticationCookie != null) { FormsAuthenticationTicket authenticationTicket = FormsAuthentication.Decrypt(authenticationCookie.Value); if (!authenticationTicket.Expired) { if (Session["UniqueUserId"] == null) { //This means for some reason the session expired before the authentication ticket. Force a login. FormsAuthentication.SignOut(); Response.Redirect("Login.aspx", true); return; } } } } }