Hi,
I need an authentication token to be thread and sync safe. The token will expire every hour and so a new one will need to be created and assigned to my static variable (TOKEN)
Will this do the trick?
Thanks,
public static volatile string TOKEN = string.Empty;
public static DateTime TOKEN_TIME = DateTime.Now;
private static readonly object syncRoot = new object();
public static string Get()
{
if (!string.IsNullOrEmpty(TOKEN))
{
if (!TokenIsValid())
{
lock(syncRoot)
TOKEN = CreateNewToken();
}
}
else
{
lock(syncRoot)
TOKEN = CreateNewToken();
}
return TOKEN;
}