0

I want to prevent users from performing actions which are more than 15 minutes apart, but I don't really need the Session object for saving any data.

Is setting SessionState timeout to 15 minutes enough, although I don't user the Session object in my code?

Is there a way to do that just through configuration, without code, or Should I put a dummy object in the Session and then rely on the timeout?

Oren A
  • 5,870
  • 6
  • 43
  • 64

1 Answers1

0

Instead of setting a global session timeout (you never know when you might want to start using session objects), you could use MemoryCache with an absolute expiration. With this generic method:

public T PreventAction<T>(string uniqueUserId, string action, DateTimeOffset absoluteExpiration, Func<T> callback) where T : class
{
    string key = uniqueUserId + action;

    T item = MemoryCache.Default.Get(key) as T;

    if (item != null)
        return null;

    MemoryCache.Default.Add(key, item, absoluteExpiration);
    return callback();
}

You could prevent a user from doing 'something' every x amount of time by implementing the following code:

// User should only be allowed to calculate 2+2 every 15 minutes

PreventAction(uniqueUserId, "Calculate2+2", DateTime.Now.AddMinutes(15), () => 
{
    var calculation = 2+2;
    Console.WriteLn(calculation);
});
Barry Gallagher
  • 6,156
  • 4
  • 26
  • 30