· 6 years ago · Nov 26, 2018, 01:14 AM
1public class OauthToken
2 {
3 public string accessToken { get; set; }
4 public string expiresIn { get; set; }
5 public DateTime created { get; set; }
6
7 public OauthToken()
8 {
9 this.accessToken = "";
10 this.expiresIn = "0";
11 this.created = new DateTime(1900, 1, 1);
12 }
13
14 public OauthToken(string accessToken, string expiresIn, string created)
15 {
16 this.update(accessToken, expiresIn, created);
17 }
18
19 public bool isValid()
20 {
21
22 try
23 {
24 //check if has expired or if it's not initialized
25 if (this.accessToken == null || this.accessToken == "")
26 {
27 return false;
28 }
29
30 double expiresInD = Double.Parse(this.expiresIn);
31
32 DateTime validUntil = this.created.AddSeconds(expiresInD);
33 if (DateTime.UtcNow < validUntil)
34 {
35 return true;
36 }
37
38 }
39 catch (Exception)
40 {
41 Console.WriteLine("failure evaluating validity of token");
42 return false;
43 }
44
45
46 return false;
47 }
48
49 public void update(string accessToken, string expiresIn, string created)
50 {
51 this.accessToken = accessToken;
52 this.expiresIn = expiresIn;
53 this.created = DateTime.Parse(created);
54 }
55 }