Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Authentication.cs 2.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  1. namespace SpeechToText.Services
  2. {
  3. using System;
  4. using System.Net.Http;
  5. using System.Threading;
  6. using System.Web.Configuration;
  7. public sealed class Authentication
  8. {
  9. // The token has an expiry time of 10 minutes https://www.microsoft.com/cognitive-services/en-us/Speech-api/documentation/API-Reference-REST/BingVoiceRecognition
  10. private const int TokenExpiryInSeconds = 600;
  11. private static readonly object LockObject;
  12. private static readonly string ApiKey;
  13. private string token;
  14. private Timer timer;
  15. static Authentication()
  16. {
  17. LockObject = new object();
  18. ApiKey = WebConfigurationManager.AppSettings["MicrosoftSpeechApiKey"];
  19. }
  20. private Authentication()
  21. {
  22. }
  23. public static Authentication Instance { get; } = new Authentication();
  24. /// <summary>
  25. /// Gets the current access token.
  26. /// </summary>
  27. /// <returns>Current access token</returns>
  28. public string GetAccessToken()
  29. {
  30. // Token will be null first time the function is called.
  31. if (this.token == null)
  32. {
  33. lock (LockObject)
  34. {
  35. // This condition will be true only once in the lifetime of the application
  36. if (this.token == null)
  37. {
  38. this.RefreshToken();
  39. }
  40. }
  41. }
  42. return this.token;
  43. }
  44. /// <summary>
  45. /// Issues a new AccessToken from the Speech Api
  46. /// </summary>
  47. /// This method couldn't be async because we are calling it inside of a lock.
  48. /// <returns>AccessToken</returns>
  49. private static string GetNewToken()
  50. {
  51. using (var client = new HttpClient())
  52. {
  53. client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ApiKey);
  54. var response = client.PostAsync("https://api.cognitive.microsoft.com/sts/v1.0/issueToken", null).Result;
  55. return response.Content.ReadAsStringAsync().Result;
  56. }
  57. }
  58. /// <summary>
  59. /// Refreshes the current token before it expires. This method will refresh the current access token.
  60. /// It will also schedule itself to run again before the newly acquired token's expiry by one minute.
  61. /// </summary>
  62. private void RefreshToken()
  63. {
  64. this.token = GetNewToken();
  65. this.timer?.Dispose();
  66. this.timer = new Timer(
  67. x => this.RefreshToken(),
  68. null,
  69. TimeSpan.FromSeconds(TokenExpiryInSeconds).Subtract(TimeSpan.FromMinutes(1)), // Specifies the delay before RefreshToken is invoked.
  70. TimeSpan.FromMilliseconds(-1)); // Indicates that this function will only run once
  71. }
  72. }
  73. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...