【发布时间】:2023-01-04 13:12:54
【问题描述】:
我有一个使用 EWS API 访问邮箱 (office365) 和阅读电子邮件附件的 SSIS 包。我正在使用 Script Task 和 C# 。我现在这样做的方式是传递邮箱的密码(基本身份验证)。根据 Microsoft 的新政策,我需要将发送的电子邮件从 EWS 转移到现代身份验证。微软将在 2022 年 10 月阻止通过 EWS 等基本协议进行身份验证。
我关注了这篇文章https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth#add-code-to-get-an-authentication-token 我现在有两个问题!我应该如何将 Microsoft.Identity.Client 4.22.0 dll 添加到脚本任务引用。通过使用 NuGet 管理,dll 不断消失! 我尝试解压缩 NuGet 包并手动添加 dll,但在运行包时仍然出现错误。
我写了以下代码:
private static async System.Threading.Tasks.Task Main(string[] args)
{
var cca = ConfidentialClientApplicationBuilder.Create(ConfigurationManager.AppSettings["appId"]).WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])
.WithTenantId(ConfigurationManager.AppSettings["tenantId"])
.Build();
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
try
{
var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();
// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
ewsClient.ImpersonatedUserId =
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "mailbox@domain.com");
//Include x-anchormailbox header
ewsClient.HttpHeaders.Add(" X-AnchorMailbox", "mailbox@domain.com");
// Make an EWS call
var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
foreach (var folder in folders)
{
Console.WriteLine($"Folder: {folder.DisplayName}");
}
}
catch (MsalException ex)
{
Console.WriteLine($"Error acquiring access token: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex}");
}
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine("Hit any key to exit...");
Console.ReadKey();
}
}
我已经将 AppID、TokenID 和 clientSecret 放在 app.config 中。
我现在遇到的问题是如何在脚本任务中从public void Main()调用static async System.Threading.Tasks.Task Main(string[] args)?我不知道我走的路是否正确!任何人都做过这个 befotr,请说明一下?
【问题讨论】:
-
您可能会考虑使用直接控制台应用程序而不是 SSIS 脚本任务。您可以直接使用 Nuget 包。
-
@KeithL 我们正试图在 SSIS 包本身内找到一个解决方案来执行此操作,因为它在阅读电子邮件附件后负责许多其他进程。
-
我无法在 SSIS 中运行任何 Nuget 包。此外,大多数 Nuget 不适用于古老的 SSIS 框架。
-
而且我绝对不认为您可以使用 async/await 或 AppConfig,除非在最新的 SSIS 中确实发生了某些变化。
标签: c# ssis oauth office365 exchangewebservices