Session实际上如何在MVC中运行? [英] How actually Session works in MVC?

查看:70
本文介绍了Session实际上如何在MVC中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC4中的会话管理感到有点困惑。



可以说,我输入了用户名和密码,然后点击了Login按钮。然后在服务器端,我从HttpContext.Current.Session获得了SessionId。然后我正在验证针对数据库的用户凭据。如果用户有效,则在Session中添加SessionId,userName和uiserId。



可以说,下次请求来自同一台机器和同一个浏览器时,我得到了相同的SessionId然后允许该用户访问其他信息。



现在我有以下问题:

1.服务器如何知道该请求是来自同一个浏览器和来自同一台机器?

2.我发现,SessionId对于不同的浏览器是不同的,但对于不同机器上的同一浏览器是相同的,所以如果我从machine1和google登录chrome,那么是否可以为不同的浏览器使用相同的会话?(意味着会话可用于具有相同浏览器的不同机器。是否可能?)

3.服务器如何理解该请求是相同的用户,谁登录?

4.在asp.net会话中由viewState维护,但是MVC中没有使用视图状态,那么MVC中使用了什么?

解决方案

1.在ASP.NET MVC应用程序中, Session 的工作方式与经典ASP.NET Web应用程序相同,据我所知,没有它们之间的区别!



2.对于尝试使用Web浏览器访问Web应用程序的每个用户==>将在该浏览器中为使用过的计算机创建并缓存新会话一段时间,因此这是因为如果您使用相同的浏览器从同一台计算机访问Web应用程序两次,则使用相同的会话。但是,如果您尝试从两台不同的计算机访问同一个Web应用程序,您将获得两个不同的Session对象,这不取决于使用的浏览器。



3。就像我上面所说的那样,Web客户端的会话被缓存一段超时时间(例如使用cookie),会话取决于使用的浏览器和计算机。所有这些会话超时和缓存参数都可以在 web.config 中配置。



4.参见上面的第1点。有关详细信息,请参阅 MSDN [ ^ ]

I'm little-bit confused about session management in MVC4.

Lets say, I entered username and password and clicked on Login button. Then on server side, I got SessionId from HttpContext.Current.Session. And then I am validating that user credentials against database. If user is valid, then Adding SessionId, userName and uiserId in Session.

Lets say, next time request is came from same machine and same browser, I got same SessionId and then allowing that user to access other information.

Now I have following questions:
1. How server come to know that request is came from same browser and from same machine?
2. I found that, SessionId is different for different browser but it is same for same browser on different machine, so If I logged in from machine1 and with google chrome, then is it possible to use same session for different browser?(means session will be available for different machine with same browser. Is it possible?)
3. How server understand that request is for same user, who is logged in?
4. In asp.net session is maintained by viewState, but view state is not used in MVC, then what is used in MVC?

解决方案

1.In ASP.NET MVC application the Session is working in the same way like in classic ASP.NET web application, and as I know there is no difference between them!

2.For each user that are trying to access the web application by using a web browser ==> a new session will be created and cached for a period of time in that browser for the used computer, so this is the reason because if you are accessing the web application from the same computer by using the same browser two times the same session is used. But if you are trying to access the same web application from two different computers you will got two different Session objects and this do not depends on the used browser.

3.Like I said above the Session for a web client is cached for a timeout period (by using cookies for example), and the session depends on the used browser and computer. All this session timeout and caching parameters are configurable in the web.config.

4.See 1st point above. For details see in MSDN[^]


这篇关于Session实际上如何在MVC中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆