Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Coding Releases > Coding Snippets
You last visited: Today at 07:13

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#] Token-Storage and Generate

Discussion on [C#] Token-Storage and Generate within the Coding Snippets forum part of the Coding Releases category.

Reply
 
Old   #1

 
Ravenstorm's Avatar
 
elite*gold: 0
The Black Market: 100/0/0
Join Date: Jan 2010
Posts: 13,150
Received Thanks: 3,206
Post [C#] Token-Storage and Generate

Tokens can be used to authenticate transactions or authorize people and even more...

Initialize your Token-Storage:
Code:
static Dictionary<string, DateTime> token_storage = new Dictionary<string, DateTime>();
Generate a Token:
Code:
byte[] actual_time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
byte[] unique_token = Guid.NewGuid().ToByteArray();
string full_token = Convert.ToBase64String(actual_time.Concat(unique_token).ToArray());
Add the Token to the Storage:
Code:
token_storage.Add(full_token,DateTime.Now);
If you have a lot of tokens in your storage you want to delete the expired ones... Check every 5 minutes if there are some expired tokens...

The timer:
Code:
timer = new Timer(1000*300);
timer.Elapsed += checkTokens;
static void timer_checkTokens(object sender, ElapsedEventArgs e)
{
    var expiredTokens = token_storage.Where(p => p.Value.AddDays(1) <= DateTime.Now).Select(p => p.Key);

    foreach (var key in expiredTokens) {
        token_storage.Remove(key);
    }
}
Ravenstorm is offline  
Thanks
5 Users
Old 11/03/2014, 00:41   #2
 
elite*gold: 1
Join Date: Aug 2013
Posts: 1,898
Received Thanks: 1,346
Shouldn't you create a List<string> from your IEnumerable<string> to get access inside your foreach loop?

Code:
  foreach (var key in expiredTokens.ToList())
            {
                TokenStorage.Remove(key);
            }
Black Tiger ツ is offline  
Old 11/08/2014, 16:59   #3

 
x]vIrus[x's Avatar
 
elite*gold: 37
Join Date: Apr 2004
Posts: 2,154
Received Thanks: 250
what is this ****, you described how to use a dictionary, how does one even need a tutorial for a dictionary?!
x]vIrus[x is offline  
Thanks
1 User
Old 11/08/2014, 19:21   #4
 
YatoDev's Avatar
 
elite*gold: 50
Join Date: Sep 2012
Posts: 3,841
Received Thanks: 1,462
Quote:
Originally Posted by x]vIrus[x View Post
what is this ****, you described how to use a dictionary, how does one even need a tutorial for a dictionary?!
it's just a snippet! calm done
YatoDev is offline  
Reply




All times are GMT +1. The time now is 07:13.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.