So if you want to selectively stop a post information based on whether a user is logged in, look at IsAuthenticated, and if you want to selectively show information based on what type of user is logged in, you use a Role provider. Because GETs can be cached, the only way to selectively allow access to an action in a GET is with AuthorizeAttribute.
You already have a good example of using TempData in your question, namely, returning a simple error message after a failed POST. In terms of what shouldbe stored in Session (beyond "not much"), I just think of Session as a user-specific cache. Like the non-user-specific Cache, you should not put security-sensitive information there
The default TempData provider uses the session so there really isn't much of a distinction, except that your TempData is cleared out at the end of the next request. You should use TempData when the data needs only to persist between two requests, preferably the second one being a redirect to avoid issues with other requests from the user -- from AJAX, for example -- deleting the data accidentally. If the data needs to persist longer than that, you should either repopulate the TempData or use the Session directly.
TempData Vs Session
TempData
|
Session
|
TempData allow us to persisting data for the duration of single subsequent request.
|
Session is able to store data much more long time, until user session is not expire.
|
ASP.net MVC will automatically expire the value of tempdata once consecutive request returned the result (it means, it alive only till the target view is fully loaded).
|
Session will be expire after the session time out occurred.
|
It valid for only current and subsequent request only
|
It valid for all requests.
|
TempData has Keep method to retention the value of TempData.
Example
TempData.Keep()
TempData.Keep(“EmpName”)
|
NA
|
TempData internally stored the value in to Session variable.
|
Session varible are stored in SessionStateItemCollection object (Which is exposed through the HttpContext.Session property of page).
|
It is used to stored only one time messages like validation messages, error messages etc.
|
It is used to stored long life data like user id, role id etc. which required throughout user session.
|
TempData and session, both required typecasting for getting data and check for null values to avoid run time exception.
|
No comments:
Post a Comment