Deal with session in ASP.NET C# - asp.net

Suppose if there are multiple sessions on single page and out of them I want to clear values of only one session then how can I achieve this. Will Session.clear() clear all sessions' value or it will create ambiguity because of multiple session?

You have only one session for one user at a time. If you clear the session, you only clear the session for the current user visiting the page, not for everybody.

Session.Clear(); // Remove all keys and values from the session state collection of current user
Session.Remove("SessionName"); //Will remove particular session variable of current user

Related

How to generate unique session id when accessing aspx page

I have a website with a number of aspx pages and one of them requires generating unique session id when an user comes in. This session id should be in time structured string as yyyymmddhhmmssms (year,month,day,hour,minute,second,milisecond). So when an user accesses to this particular aspx page, the user gets an unique session id. Then when finish button is clicked the session id is fired and will never be reused.
This might be possible, but first things first: A timestamp isn't unique, and you don't ever want sessionIDs to be guessable. That's why default SessionIDs are 120-bit numbers that are generated by a cryptographic random number generator (details here). Anything deterministic like a timestamp should be avoided unless you and your users are ok with the contents of a session being available to the entire world.
More about session state security is here.
Anyway, I'm not aware of a way to swap out the session ID for a single page. Your browser associates a session cookie with a host, not an individual page, so it'll send your session cookie to every page on your site. If you're not using sessions anywhere else on your site then you could get away with the following:
Disable session state for every page except the one in question by using the Page directive's EnableSessionState attribute. When the user clicks the finish button, call Session.Abandon in the handler.
Use your own session ID generator by implementing and registering your own ISessionIDManager implementation. But please, please don't use a timestamp for your IDs.

Using SQL Server Trigger for updating old rows on Insert

I have users table containing all the user account details including a IsLoggedIn field with bit data type. I want to update any old rows with IsLoggedIn = 1 when a user starts new session before previous session expires. What is the best way to achieve this? Is it possible to do this with Trigger?
Thanks in advance..
You will have two things for sure.. 1) User ID and 2) Session Id in database.
Now when new session start (session_Start) event gets fire.. you can call one store procedure with logic and the logic will be
1) If there is not records with same userID do not do any update.
2) If there are any records with same userID update all of them with IsLoggedIn = 1.
You can extend this logic with TRIGGER as well and band it as per your need,.
Basically you need to track session_Start event and perform DB operations.
Trigger won't be the suggested solution as you need to handle login from UI. If there is going to be only a single login then Application_Start can be used. If there can be multiple logins then Session_Start is preferable. From there you can give a call to database and perform the respective actions.
Difference between the two is explained at : http://forums.asp.net/t/1230163.aspx/1?What+s+the+difference+between+Application_Start+and+Session_Start+in+Global+aspx+

What is the best way to add a session variable from the database in asp.net?

In my asp.net MVC application, I want to add a variable (a flag for the kind of system a user has) that can be accessed whenever a page loads or the user performs certain actions. I have decided to add a session variable for this (does this seem reasonable?), and I just need to grab the flag from a table in the database. My plan was to set the variable on Session_Start, but this doesn't appear to be the right way to do it as I need to query the database and I'm not sure if I should from the Global.asax. Where should I be populating this variable? Or is there a better way to do it?
Thanks in advance!
Session variable is a reasonable choice and session_start can be a place to get the value. However, if the value is user specific then in such case, you need user identity. Authentication will establish user's identity and not session start (both are independent in ASP.NET) - so instead of session_start, better bet would be Application_AcquireRequestState where you should check if user is authenticated or not and if yes, then check if your session variable has been set or not. If not set then you can get the value from database.
A slight variation would be on-demand loading i.e. create a wrapper method to get the flag value. Wrapper method will check if value has already been retrieved or not - if not then it will fetch it and cache the value in suitable store (e.g. session state).

ASP.NET MVC, incrementing a value in the model's data every time a user clicks on a link

Essentially, this is what I have:
I have a view form for an object in MVC. On this form, I have a hyperlink. Every time that the user clicks the hyperlink, I would like an integer value inside of the object to increase by one, in addition to redirecting the user to a different page.
Is there a way to do this and keep track of how many times the link is clicked?
Thanks for any help that you can offer!
Yeah. Just have the link point to an MVC action that increments the value you want to increment before redirecting them to the action that they should actually go to.
Without more information, that's about as detailed as I can get.
The options are many. If it is one user then you could track this in any of the following: session, cookie, querystring, database or even LocalStorage.
If "the user" is just any user then cookies and LocalStorage are ruled out.
I'd imagine session is the easiest, assuming scale is not a major concern.
You have multiple options for this.
-> Save it in database
-> Session
-> increment value in the global variable

storing user preferences

In my pages, the first thing I do when a page is being displayed is query a table called user preferences. I'm doing this for every page and I was wondering what are the options available to avoid repeating this query as the user jumps from page to page on my application.
Thanks.
You should either store the user preference in a Cookie or Session.
Some good reads for you:
http://wiki.asp.net/page.aspx/57/session/
and
http://msdn.microsoft.com/en-us/library/aa289495(v=vs.71).aspx
If both cases, check if the value is not null. If it is not null, use the Cookie or Session value. If it is null, populate your Session / Cookie value with the result of your database call.
This way you will only make this DB call once.

Resources