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;
}
You should be using something like below in GenerateToken method
This will generate time with even minutes which is same for 2 minutes duration.
UPDATE
Full Code