none
Token: How to generate token which will valid up to 5 minutes RRS feed

  • Question

  • i am looking for a sample which generate a token number which valid for 5 minutes. share a code for this as example.

    here i got a sample code which generate a token which valid upto 24 hours but its code and logic not clear to me that how it generate a number which only valid for 24 hours. so please see the code and tell me how this code works.

    public static string GenerateToken()
    {
    	int month = DateTime.Now.Month;
    	int day = DateTime.Now.Day;
    	string token = ((day * 100 + month) * 700 + day * 13).ToString();
    	return token;
    }

    Please tell me how the above code generate a number which exist as a valid token for next 24 hours? please discuss the above code how it works?

    thanks

    thanks

    Sunday, July 5, 2020 6:02 PM

Answers

  • Hi Sudip_inn,
    First, you can create a unique token containing a time stamp via following code:

    byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
    byte[] key = Guid.NewGuid().ToByteArray();
    string token = Convert.ToBase64String(time.Concat(key).ToArray());

    Then you need to decode the token to get the creation time:

    byte[] data = Convert.FromBase64String(token);
    DateTime when = DateTime.FromBinary(BitConverter.ToInt64(data, 0));
    if (when < DateTime.UtcNow.AddMinutes(-5))
    {
       //
    }

    Here is a simliar thread you can refer to.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 8, 2020 9:10 AM
    Wednesday, July 8, 2020 8:22 AM
  • Hi Sudip_inn,
    First,  the DateTime.FromBinary method  is used to deserializes a 64-bit binary value and recreates an original serialized DateTime object.
    And "data" includes time and key. "0" means that you extract the time from date and it is startIndex.
    For a deeper understanding, please refer to the following document.
    [BitConverter.ToInt64 Method]
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 8, 2020 5:30 PM
    Wednesday, July 8, 2020 9:56 AM

All replies

  •   

    Hi Sudip_inn,

    the above code generate number that exist or valid up for next hours because the  DateTime.Now.Day
    automatically increment day by day so one day is 24 hours !

    for example 

    1- Current month = 7

    2- Current day = 6

    3- ((6 * 100 + 7)) * 700 + 6 * 13)  =  424978  this token is valid for 24 hours

    for next day the Current day would be 7  

    So,   ((7 * 100 + 7)) * 700 + 7 * 13)  =  494991


    Please remember to mark the replies as answers if they helped you :) ~

    Sunday, July 5, 2020 9:33 PM
  • This calculation is not clear

    ((6 * 100 + 7)) * 700 + 6 * 13)

    why we are multiply 100 with current day?

    and why we add current 7 with 6*100 ?

    please discuss this calculation why there are so many multiplication and addition ? is it required ?

    if i need to generate a token which will valid upto 5 minute then what will be code ?

    thanks

    Tuesday, July 7, 2020 1:13 PM
  • please share small routine which generate a arbitrary number which expire after 5 minutes. i do not want to store anything in db and also not interested to use identity server to generate token.

    thanks

    Tuesday, July 7, 2020 5:20 PM
  • Hi Sudip_inn,
    First, you can create a unique token containing a time stamp via following code:

    byte[] time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
    byte[] key = Guid.NewGuid().ToByteArray();
    string token = Convert.ToBase64String(time.Concat(key).ToArray());

    Then you need to decode the token to get the creation time:

    byte[] data = Convert.FromBase64String(token);
    DateTime when = DateTime.FromBinary(BitConverter.ToInt64(data, 0));
    if (when < DateTime.UtcNow.AddMinutes(-5))
    {
       //
    }

    Here is a simliar thread you can refer to.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 8, 2020 9:10 AM
    Wednesday, July 8, 2020 8:22 AM
  • Sir please explain this line 

    DateTime when = DateTime.FromBinary(BitConverter.ToInt64(data, 0));

    what DateTime.FromBinary does ?

    what is the meaning of this line BitConverter.ToInt64(data, 0) ? is it trying to extract anything? why 0 is used here?

    thanks

    Wednesday, July 8, 2020 9:12 AM
  • Hi Sudip_inn,
    First,  the DateTime.FromBinary method  is used to deserializes a 64-bit binary value and recreates an original serialized DateTime object.
    And "data" includes time and key. "0" means that you extract the time from date and it is startIndex.
    For a deeper understanding, please refer to the following document.
    [BitConverter.ToInt64 Method]
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Wednesday, July 8, 2020 5:30 PM
    Wednesday, July 8, 2020 9:56 AM
  • one more request can we generate token like arbitrary number say like 635017 which will be valid for next 5 minute?


    i just use your code and saw control not coming into this block....why

    if (when < DateTime.UtcNow.AddMinutes(-5))
                {
                    //
                }

    then how to check token is valid or not ?

    please guide me. thanks


     
    Thursday, July 9, 2020 9:40 AM
  • Here i share @daniel code

    using System;
    using System.Linq;
    using System.Threading;

    public class Program
    {
    public static void Main()
    {
    var ogTime = DateTime.UtcNow;
    Console.WriteLine("ORIGINAL DATETIME: {0}",  ogTime);
    Console.WriteLine("___________________________________________\n");

    // ==== CREATE A TOKEN W/ A DATETIME IN IT ====

    // Convert our DateTime object into bytes
    byte[] time = BitConverter.GetBytes(ogTime.ToBinary());

    Console.WriteLine("ORIGINAL DATETIME BYTES: {0}\n",  BitConverter.ToString(time));

    // Bake the DateTime into a GUID
    byte[] key = Guid.NewGuid().ToByteArray();
    string token = Convert.ToBase64String(time.Concat(key).ToArray());

    Console.WriteLine("STRING TOKEN: {0}\n",  token);


    // ==== NOW LET'S REVERSE THE PROCESS TO GET THE DATETIME BACK ====

    // Deserialize the token with our DateTime in it
    byte[] tokenBytes = Convert.FromBase64String(token);

    // Convert the bytes into a 64-bit signed integer
    long arg = BitConverter.ToInt64(tokenBytes, 0);
    Console.WriteLine("64-BIT INT ARG FOR DATETIME.FROMBINARY(): {0}\n",  arg);

    // Feed the 64-bit signed integer into a static DateTime method 
    // to rebuild a copy of our original DateTime object
    DateTime rebuiltDateTime = DateTime.FromBinary(arg);

    Console.WriteLine("___________________________________________\n");
    Console.WriteLine("REBUILT DATETIME: {0}\n",  rebuiltDateTime);

    // Now you can write any time requirement conditions against rebuilt DateTime that you need for validation
    var seconds = 4;

    Thread.Sleep(3000);

    if (rebuiltDateTime > DateTime.UtcNow.AddSeconds(-seconds))
    {
    Console.WriteLine("It has been LESS than 4 seconds since the token was generated.");

    // Do stuff 
    }

    if (rebuiltDateTime < DateTime.UtcNow.AddSeconds(-seconds))
    {
    Console.WriteLine("It has been MORE than 4 seconds since the token was generated.");

    // Do stuff 
    }
    }
    }

    Friday, July 10, 2020 6:44 AM