Take the 2-minute tour ×

I have a requirement where I need to create a database record at the start of a user session on my MVC 4 web site.

The outline of the entry point controller looks like this:

public ActionResult Index(int userId, int requestId) {
   var userInfo = db.GetUserInfo(userId, requestId);

   if (userInfo == null) {
      userInfo = db.CreateUserInfo(userId, requestId);

   // rest of code

What I have found is that this code is not fully reliable in as much that very ocassionaly a duplicate record is created (typically with the same creation date - at least to the nearest millisecond). I can only assume that the (external) web page/client generates two, almost simultaneous, requests.

Note, if a user returns to the site at a later date (using the same query string parameters), then I want them to pick up the existing userInfo record.

I know I could 'fix' the problem by adding a unique index to the table, which I probably need to do anyway, but I would like to know if there is a better pattern to follow?

share|improve this question
This type of code my be better suited adding it to the Global.asax file in the Session_start event. This way you can capture users who come in at other places than the initial entry point. –  Justin Chmura Jan 16 '13 at 17:56
What's the intent behind putting ! in the if condition? –  tigeronk2 Jan 16 '13 at 17:59
The if (!userInfo == null) is there to check if the record already exists. If userInfo is null then a new record is required. –  Neilski Jan 16 '13 at 18:03
Wouldn't if(userInfo == null) suffice? –  tigeronk2 Jan 16 '13 at 18:09
Sorry, yes. Mistake in code - I've corrected the example now –  Neilski Jan 16 '13 at 19:28

1 Answer 1

up vote 1 down vote accepted

Generate a random string for the user session when he will register and store it in the userInfo table in your database. When the user will come to the site at a later date , you will just check the username and password for authentication and if it is successful, then you will get that previously stored unique random string from the database for that user and put it in the session. This way you can use the same random string throughout the lifetime off user. hope this helps

share|improve this answer
The problem is that it is the combination of the query string parameters that describe the uniqueness of the record. It is possible that a user might return to the site at a later date. In this case I need them to be able to pick up the existing userInfo record. –  Neilski Jan 16 '13 at 18:06
@Neilski Updated my answer...... –  Bhushan Firake Jan 16 '13 at 18:13
Thanks Bhushan. Isn't it possible though that since I am, I assume, receiving two initial request, isn't it also possible that two sessions will also be created? –  Neilski Jan 16 '13 at 18:38
@Neilski No , only one session will be created because for second request , you will fetch random string stored for first session in database. –  Bhushan Firake Jan 17 '13 at 2:02

Your Answer


