I use the collection AdministratorSessionfor my sessions. When he clicks Remember Me, I want the session document to be deleted after 3600 minutes, otherwise 30 minutes. Is it possible? If not, what are my options?
I tried using a simple approach right here, but it creates an index and sets a TTL value for the first document created. For example, if one ttl document is set to 30 and next to 3600, they will both be deleted after 30 minutes.
Here is how I am doing it now.
public AdministratorSession Add(string ip, bool remember)
{
var random = new Random();
var session = new AdministratorSession
{
StartDateTime = DateTime.Now,
Hash = EncryptionService.Sha256(DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture)),
Salt = EncryptionService.Md5(ip + random.Next(0, 1000))
};
var db = DbContext.GetDatabase();
var collection = db.GetCollection<AdministratorSession>("AdministratorSession");
collection.EnsureIndex(IndexKeys.Ascending("StartDateTime"), IndexOptions.SetTimeToLive(TimeSpan.FromMinutes(remember ? 36000 : 30)));
collection.Insert(session);
return session;
}
source
share