MVC为网站处理CorpId

我不确定我是以正确的方式处理这个问题,但由于我遇到了问题,我认为我不是。

我必须在加载登录屏幕时发送公司ID。

看起来像这样:

public ActionResult LogOn(string id) { var sb = new StringBuilder(); sb.AppendLine(string.Format("CorpID: {0}", id)); if(ViewBag.CorpID != null) sb.AppendLine(string.Format("ViewBag.CorpID: {0}", ViewBag.CorpID)); Guid corpIdGuid; if (!Guid.TryParse(id, out corpIdGuid) && string.IsNullOrEmpty(ViewBag.CorpID)) return null; // the id passed in will take presidence over the // viewbag unless it is blank then we use viewbag // one way or the other viewbag.corpid should not // be blank if(!string.IsNullOrEmpty(id)) ViewBag.CorpID = id; // Session["CorpId"] = id; //Not a junk guid.. continue. return View(); } 

我需要这个来确定我们将在本届会议期间与哪家公司合作。

我遇到的问题是,当cookie超时发生时,设置为10分钟,它会将它们引导回此登录状态,我再也没有了。

我试过了viewbag ,它正在被重置。

我试过一个cookie,但是因为它过期了,数据就不再存在了。

我尝试了一个配置文件管理器,但由于它们已经记录下来,这让我一无所获。

当用户超时并重新登录登录屏幕时,如何维护此CorpId ? 我也需要每个屏幕的这些信息。

任何投入将不胜感激!

您需要创建一个单独的cookie,用于标识未与用户会话过期的公司IDSession["CorpId"]将与用户会话一起到期,但不起作用。

 var corpCookie = new HttpCookie("CorpID", id); corpCookie.Expires = DateTime.Now.AddDays(30.0); HttpContext.Current.Response.Cookies.Set(corpCookie); 

每次用户登录时,您都可以更新到期日以使其过期。 要检索cookie值,请使用以下命令:

 var corpID = HttpContext.Current.Request.Cookies.Get("CorpID").Value;