How to lock the token, Based on the time in asp.net?

Am using the following program for generate the Token,based on the time(current time).It was work properly,It means the token will be generate.

         public string GenerateToken()
            {
                MD5 md5 = System.Security.Cryptography.MD5.Create();
                DateTime input = DateTime.Now;
                byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input.ToString());
                byte[] hash = md5.ComputeHash(inputBytes);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    sb.Append(hash[i].ToString("X2"));
                }
                return sb.ToString();
            }

And I use the following program for try to lock(expire) the token with in 2 minutes.It means I access the GetToken() method, It will return the token for particular Time(current Time).I need to set valid time 2 minutes for that token.Incase I access the getToken() before 2 minutes I need to get the same token.But I access the getToken() after the 2 minutes i need to get new token. How can I do this?How do lock the token?

     public string GetToken()
        {
            DateTime currentTime = DateTime.UtcNow;               
            tokenExp = DateTime.UtcNow.AddMinutes(2);
            if (tokenExp >= currentTime)
            {
                token = GenerateToken();
                return token;
            }

            else lock (tokenLock)
                {
                    if (tokenExp >= currentTime)
                    {
                        token = GenerateToken();
                        //return token;

                    }
                    return token;
                }

Answers


You should be using something like below in GenerateToken method

 DateTime dateTime = DateTime.UtcNow;
 dateTime = dateTime.AddSeconds(-dateTime.Second);
 if (dateTime.Minute % 2 != 0)
     dateTime = dateTime.AddMinutes(1);

This will generate time with even minutes which is same for 2 minutes duration.

UPDATE

Full Code

public string GenerateToken()
{
    MD5 md5 = System.Security.Cryptography.MD5.Create();
    DateTime dateTime = DateTime.UtcNow;
    dateTime = dateTime.AddSeconds(-dateTime.Second);
    if (dateTime.Minute % 2 != 0)
        dateTime = dateTime.AddMinutes(1);

    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(dateTime.ToString());
    byte[] hash = md5.ComputeHash(inputBytes);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("X2"));
    }
    return sb.ToString();
}

You are using hashing while you should use encryption.

Hashing is one-way but encryption is two way i.e. when you get back the token, you can decrypt the token to get the original value to be able to calculate time difference and check if it is > 2 minutes.

You can use AES, 3DES, ...


Need Your Help

ASP.NET calendar shows 6 weeks, how can i make it display 5?

asp.net

The default asp.net calendar renders with 6 displayable weeks, the last week is always the 'next' month and i dont care about it. Is there a way to display only 5 weeks?