简体   繁体   English

ASP.NET MVC 3 - 处理会话变量

[英]ASP.NET MVC 3 - Dealing with Session variables

I have an app which uses Form's Authentication and when the user log's in, I retrieve the user's actual name and assign that to a session variable, like so: 我有一个使用Form的身份验证的应用程序,当用户登录时,我检索用户的实际名称并将其分配给会话变量,如下所示:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            Session["Name"] = client.GetName(model.UserName);
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            return RedirectToAction("Index", "Home"); 
        }
    }
}

This is then displayed on my Index view, like so: 然后,它会显示在我的索引视图中,如下所示:

<h3>Welcome, @Session["Name"]</h3>

So if my name was Bob, it would output "Welcome, Bob" on my view and this works fine. 所以,如果我的名字是鲍勃,它会在我的视图中输出“欢迎,鲍勃”,这很好。 But once I navigate away from the page or close my browser and return a few minutes later, it seems as if these Session variables have been destroyed as it just outputs "Welcome, " but I'm still logged in so my session isnt destroyed? 但是,一旦我离开页面或关闭我的浏览器并在几分钟后返回,似乎这些Session变量已被销毁,因为它只是输出“欢迎”,但我仍然登录,所以我的会话没有被销毁? I've set the session to be destroyed after 60 minutes in my web.config: 我在web.config中将会话设置为在60分钟后销毁:

<sessionState regenerateExpiredSessionId="true" timeout="60" />

Edit 编辑

This only happens when I check my "Remember Me" box when logging in, as I guess this keeps a cookie client side so when I re-open my browser Im still logged in but a new session ID is created as I did a Response.Write(Session.SessionID) on my Index page and the ID before I closed my browser was different to the one when I re-opened it. 这只发生在我登录时检查“记住我”框时,因为我猜这会保留一个cookie客户端,所以当我重新打开浏览器时我仍然登录但是创建一个新的会话ID,因为我做了一个Response.Write(Session.SessionID)在我关闭浏览器之前,在我的索引页面上Response.Write(Session.SessionID)和ID与我重新打开它时的ID不同。 If I don't check my "Remember Me" box then I'm forced to login again upon re-opening my browser 如果我不检查我的“记住我”框,那么我将在重新打开浏览器后再次登录

I had the same problem with my session variables. 我的会话变量遇到了同样的问题。 If the remember me option was selected at the logon it would bypass my code to set the session variable I needed the next time the user would go to the site. 如果在登录时选择了“记住我”选项,它将绕过我的代码以设置下次用户访问该站点时所需的会话变量。

I was able to solve my issue by repopulating the session variable if the IsAuthenticated was true. 如果IsAuthenticated为true,我可以通过重新填充会话变量来解决我的问题。

protected void Session_Start(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        Session["Name"] = client.GetName(User.Identity.Name);   
    }
}

Instead of adding the name to a session variable, just change the following 不要将名称添加到会话变量,只需更改以下内容即可

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

to

FormsAuthentication.SetAuthCookie(client.GetName(model.UserName), model.RememberMe);

You can then just use the User.Identity.Name instead of the @Session["Name"]. 然后,您可以使用User.Identity.Name而不是@Session [“Name”]。

The issue you have is with the line 你遇到的问题是线路问题

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

This is a cookie and last longer than sessions (depending on how long you set the forms timeout) 这是一个cookie,持续时间比会话长(取决于您设置表单超时的时间)

If all you need is to just display the username, you can use and just remove the session altogether 如果您只需要显示用户名,则可以使用并完全删除会话

<h3>Welcome, @User.Identity.Name</h3>
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 

this code should work fine and you should be able to see "Welcome USERNAME" , try to see that whether IE settings like tools-->internet options-->General tab delete my browsing history is checked or not. 此代码应该可以正常工作,您应该能够看到"Welcome USERNAME" ,尝试查看IE设置是否像tools-->internet options-->General选项卡delete my browsing history是否已选中。 (on the same tab is you click on delete button you will see its clearing cookies also so that might be issue). (在同一个选项卡上,您单击删除按钮,您将看到其清除cookie也可能是问题)。

Cookies values will be retained if you close browser but not session(inproc) variables. 如果您关闭浏览器而不是会话(inproc)变量,将保留Cookie值。

Maybe first check to ensure that a new session isn't started somehow. 也许首先检查以确保不会以某种方式启动新会话。 Place a breakpoint in the Session_Start in the global.asax.cs file: global.asax.cs文件的Session_Start中放置一个断点:

protected void Session_Start(object sender, EventArgs e)
{
    var sessionId = Session.SessionID; // break here
}

It might seem silly but there are a couple of things that could actually cause a new session. 这可能看起来很愚蠢,但有几件事实际上可能导致新的会话。 Eliminating those will get you closer to a solution. 消除这些将使您更接近解决方案。

Closing your browser and opening it up again will probably cause a new session. 关闭浏览器并再次打开它可能会导致新的会话。 Changes to the folder structure within your site and changes to the web.config will cause a new session (application pool will be recycled). 对站点中文件夹结构的更改以及对web.config的更改将导致新会话(应用程序池将被回收)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM