MicrosoftGraphMailboxService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Net.Http.Headers;
  2. using System.Text;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. using Microsoft.Extensions.Options;
  6. using Microsoft.Identity.Client;
  7. using Microsoft.Identity.Client.Extensions.Msal;
  8. namespace OASystem.API.OAMethodLib.MicrosoftGraphMailbox;
  9. public class MicrosoftGraphMailboxService : IMicrosoftGraphMailboxService
  10. {
  11. private static readonly string[] Scopes =
  12. {
  13. "Mail.Read",
  14. "User.Read",
  15. "Mail.Send"
  16. };
  17. private readonly IHttpClientFactory _httpClientFactory;
  18. private readonly IOptionsMonitor<MicrosoftGraphMailboxOptions> _options;
  19. private readonly ILogger<MicrosoftGraphMailboxService> _logger;
  20. private readonly SemaphoreSlim _initLock = new(1, 1);
  21. private readonly SemaphoreSlim _tokenLock = new(1, 1);
  22. private IPublicClientApplication? _pca;
  23. private MsalCacheHelper? _cacheHelper;
  24. private const string HttpClientName = "MicrosoftGraph";
  25. public MicrosoftGraphMailboxService(
  26. IHttpClientFactory httpClientFactory,
  27. IOptionsMonitor<MicrosoftGraphMailboxOptions> options,
  28. ILogger<MicrosoftGraphMailboxService> logger)
  29. {
  30. _httpClientFactory = httpClientFactory;
  31. _options = options;
  32. _logger = logger;
  33. }
  34. public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToken = default)
  35. {
  36. await EnsureInitializedAsync(cancellationToken).ConfigureAwait(false);
  37. await _tokenLock.WaitAsync(cancellationToken).ConfigureAwait(false);
  38. try
  39. {
  40. var app = _pca ?? throw new InvalidOperationException("MSAL 未初始化。");
  41. var accounts = await app.GetAccountsAsync().ConfigureAwait(false);
  42. var account = accounts.FirstOrDefault();
  43. try
  44. {
  45. var result = await app.AcquireTokenSilent(Scopes, account)
  46. .ExecuteAsync(cancellationToken)
  47. .ConfigureAwait(false);
  48. return result.AccessToken;
  49. }
  50. catch (MsalUiRequiredException)
  51. {
  52. _logger.LogInformation("Graph 邮箱:需要交互式登录(将打开浏览器),重定向: {Redirect}",
  53. _options.CurrentValue.RedirectUri);
  54. var result = await app.AcquireTokenInteractive(Scopes)
  55. .WithPrompt(Prompt.SelectAccount)
  56. .ExecuteAsync(cancellationToken)
  57. .ConfigureAwait(false);
  58. return result.AccessToken;
  59. }
  60. }
  61. finally
  62. {
  63. _tokenLock.Release();
  64. }
  65. }
  66. public async Task<string?> GetMeRawJsonAsync(CancellationToken cancellationToken = default)
  67. {
  68. using var client = await CreateAuthenticatedClientAsync(cancellationToken).ConfigureAwait(false);
  69. using var response = await client.GetAsync("me", cancellationToken).ConfigureAwait(false);
  70. var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
  71. if (!response.IsSuccessStatusCode)
  72. _logger.LogWarning("Graph GET /me 失败: {Status} {Body}", (int)response.StatusCode, body);
  73. return body;
  74. }
  75. public async Task<string?> GetInboxMessagesJsonSinceAsync(DateTime startUtc, CancellationToken cancellationToken = default)
  76. {
  77. var opt = _options.CurrentValue;
  78. var startTime = startUtc.ToString("o");
  79. var url =
  80. "me/mailFolders/inbox/messages" +
  81. "?$select=id,subject,from,receivedDateTime,bodyPreview,conversationId" +
  82. $"&$filter=receivedDateTime ge {startTime}" +
  83. "&$orderby=receivedDateTime desc" +
  84. $"&$top={opt.TopMessages}";
  85. using var client = await CreateAuthenticatedClientAsync(cancellationToken).ConfigureAwait(false);
  86. using var response = await client.GetAsync(url, cancellationToken).ConfigureAwait(false);
  87. var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
  88. if (!response.IsSuccessStatusCode)
  89. _logger.LogWarning("Graph 拉取收件箱失败: {Status} {Body}", (int)response.StatusCode, body);
  90. return body;
  91. }
  92. public async Task SendMailAsync(string toEmail, string subject, string textBody, CancellationToken cancellationToken = default)
  93. {
  94. var payload = new GraphSendMailRequest
  95. {
  96. Message = new GraphSendMailMessage
  97. {
  98. Subject = subject,
  99. Body = new GraphSendMailBody
  100. {
  101. ContentType = "Text",
  102. Content = textBody
  103. },
  104. ToRecipients = new List<GraphSendMailRecipient>
  105. {
  106. new()
  107. {
  108. EmailAddress = new GraphSendMailEmail { Address = toEmail }
  109. }
  110. }
  111. },
  112. SaveToSentItems = true
  113. };
  114. var json = System.Text.Json.JsonSerializer.Serialize(payload, new JsonSerializerOptions
  115. {
  116. DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
  117. });
  118. using var client = await CreateAuthenticatedClientAsync(cancellationToken).ConfigureAwait(false);
  119. using var content = new StringContent(json, Encoding.UTF8, "application/json");
  120. using var response = await client.PostAsync("me/sendMail", content, cancellationToken).ConfigureAwait(false);
  121. var responseText = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
  122. if (!response.IsSuccessStatusCode)
  123. {
  124. _logger.LogError("Graph sendMail 失败: {Status} {Body}", (int)response.StatusCode, responseText);
  125. response.EnsureSuccessStatusCode();
  126. }
  127. }
  128. private async Task<HttpClient> CreateAuthenticatedClientAsync(CancellationToken cancellationToken)
  129. {
  130. var token = await GetAccessTokenAsync(cancellationToken).ConfigureAwait(false);
  131. var client = _httpClientFactory.CreateClient(HttpClientName);
  132. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
  133. return client;
  134. }
  135. private async Task EnsureInitializedAsync(CancellationToken cancellationToken)
  136. {
  137. if (_pca != null)
  138. return;
  139. await _initLock.WaitAsync(cancellationToken).ConfigureAwait(false);
  140. try
  141. {
  142. if (_pca != null)
  143. return;
  144. var opt = _options.CurrentValue;
  145. if (string.IsNullOrWhiteSpace(opt.ClientId))
  146. throw new InvalidOperationException("MicrosoftGraphMailbox:ClientId 未配置。");
  147. var cacheDir = string.IsNullOrWhiteSpace(opt.CacheDirectory)
  148. ? Path.Combine(
  149. Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
  150. "OASystem",
  151. "MicrosoftGraphMailbox")
  152. : opt.CacheDirectory;
  153. Directory.CreateDirectory(cacheDir);
  154. var storage = new StorageCreationPropertiesBuilder(opt.CacheFileName, cacheDir)
  155. .Build();
  156. _cacheHelper = await MsalCacheHelper.CreateAsync(storage).ConfigureAwait(false);
  157. _pca = PublicClientApplicationBuilder
  158. .Create(opt.ClientId)
  159. .WithAuthority($"https://login.microsoftonline.com/{opt.Tenant}")
  160. .WithRedirectUri(opt.RedirectUri)
  161. .Build();
  162. _cacheHelper.RegisterCache(_pca.UserTokenCache);
  163. _logger.LogInformation("Graph 邮箱 MSAL 已初始化,缓存目录: {Dir}", cacheDir);
  164. }
  165. finally
  166. {
  167. _initLock.Release();
  168. }
  169. }
  170. }