Guest User

Untitled

a guest
Oct 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace nUnit_RestTests.Utils
  7. {
  8. class TokenProvider
  9. {
  10. private static string token = null;
  11. private static DateTime tokenRenewTime = DateTime.MinValue;
  12. private static TimeSpan maxTimespan = new TimeSpan(5, 0, 0);
  13.  
  14. public static string getToken()
  15. {
  16. if (token != null)
  17. {
  18. if ((DateTime.Now - tokenRenewTime) > maxTimespan)
  19. renewToken();
  20. }
  21. else
  22. {
  23. renewToken();
  24. }
  25.  
  26.  
  27. return token;
  28. }
  29.  
  30. private static void renewToken()
  31. {
  32. if ((String.IsNullOrEmpty(TestRunner.configReader.Login)) || (String.IsNullOrEmpty(TestRunner.configReader.Password)) || (String.IsNullOrEmpty(TestRunner.configReader.AuthURL))) {
  33. token = null;
  34. return;
  35. }
  36.  
  37. string authRequest = string.Format("{0}rest/Authentication?request=getToken&username={1}&password={2}&f=HTML", TestRunner.configReader.AuthURL, TestRunner.configReader.Login, TestRunner.configReader.Password);
  38. token = TestRunner.getSynchronous(authRequest);
  39. tokenRenewTime = DateTime.Now;
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment