将实验性 ADAL 构建中的代码调整为最新的 ADAL 构建

Adapt code from experimental ADAL build to the newest ADAL build

我正在使用 Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory Nuget package for authentication to Outlook.com API, however, like a month ago all the members defined in OutlookServicesClient.Me drastically stopped working for unknown reason (like: OutlookServicesClient.Me.MailFolders.Where(predicate).ExecuteSingleAsync()) throwing a kind of server petition failed error message with no useful info, so for that reason I would like to try the regular library: Microsoft.IdentityModel.Clients.ActiveDirectory 看看它是否可以自动解决问题,而不是尝试使用实验版本。

我遇到的问题是尝试迁移下面的下一个代码,它基于实验版本的语法。我没有发现 AcquireTokenAsync 函数的任何重载,我可以在其上指定 Outlook 范围,我也没有找到其他方法,也是第一个参数AcquireTokenAsync 函数需要一个 resource 字符串参数,我不确定我必须把什么放在那里。

我看到了使用 AcquireTokenAsync 的官方和第 3 方示例,但我在尝试调整它们时感到困惑,因为 resource 参数或其他我不确定它们如何工作的东西, 另外,我看到的很多例子都是针对 ASP.NET,但我使用的是 Windows Forms.


这是我需要 migrate/update 新的 ADAL 语法和方法重载的代码:

VB.NET版本(原码)

Dim scopes As String() = {
    "https://outlook.office.com/mail.read",
    "https://outlook.office.com/mail.readwrite",
    "https://outlook.office.com/mail.send"
}

Dim cache As New TokenCache()
Dim authContext As New AuthenticationContext("https://login.microsoftonline.com/common", cache)
Dim authResult As AuthenticationResult = 
    Await authContext.AcquireTokenAsync(scopes, Nothing, "*Client ID*",
                                        New Uri("urn:ietf:wg:oauth:2.0:oob"),
                                        New PlatformParameters(PromptBehavior.Auto))

Dim client As New OutlookServicesClient(New Uri("https://outlook.office.com/api/v2.0"), 
                                        Function() Task.FromResult(Me.authResult.Token))

C#版本(未经测试在线翻译)

string[] scopes = {
    "https://outlook.office.com/mail.read",
    "https://outlook.office.com/mail.readwrite",
    "https://outlook.office.com/mail.send"
};

TokenCache cache = new TokenCache();

AuthenticationContext authContext = 
    new AuthenticationContext("https://login.microsoftonline.com/common", cache);

AuthenticationResult authResult = 
    Await authContext.AcquireTokenAsync(scopes, null, "*Client ID*", 
                                        new Uri("urn:ietf:wg:oauth:2.0:oob"), 
                                        new PlatformParameters(PromptBehavior.Auto));

OutlookServicesClient client = 
    new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"), 
                              () => Task.FromResult(authResult.Token));

这是我的翻译意图,它不起作用首先是因为我不知道在哪里设置三个 Outlook 范围 url,其次是因为当我接受微软的许可时Web 表单显示由于请求无效而无法处理。 (我设置为 Nothing/nul resource 参数)

VB.NET版本(原码)

Dim cache As New TokenCache()
Dim authContext As New AuthenticationContext("https://login.microsoftonline.com/common", cache)
Dim authResult As AuthenticationResult = 
    Await authContext.AcquireTokenAsync(resource???, "*Client ID*", 
                                        New Uri("urn:ietf:wg:oauth:2.0:oob"),
                                        New PlatformParameters(PromptBehavior.Auto))

Dim client As New OutlookServicesClient(New Uri("https://outlook.office.com/api/v2.0"), 
                                        Function() Task.FromResult(authResult.AccessToken))

C#版本(未经测试在线翻译)

TokenCache cache = new TokenCache();

AuthenticationContext authContext = 
    new AuthenticationContext("https://login.microsoftonline.com/common", cache);

AuthenticationResult authResult = 
    Await authContext.AcquireTokenAsync(resource???, "*Client ID*", 
                                        new Uri("urn:ietf:wg:oauth:2.0:oob"), 
                                        new PlatformParameters(PromptBehavior.Auto));

OutlookServicesClient client = 
    new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"), 
                              () => Task.FromResult(authResult.AccessToken));

要使用 V2 终结点对客户端应用程序进行身份验证,我们可以使用 MSAL 库。

这里有一个例子供您参考:

通过 NuGet 安装包: Install-Package Microsoft.Identity.Client -pre

检索令牌的示例代码:

   public static async void GetToken()
    {
        string clientId = "0cfa4d3e-db48-400f-9b44-901cd5975312";
        var app = new PublicClientApplication(clientId);
        AuthenticationResult result = null;
        try
        {
            string[] scopes = {
                    "https://outlook.office.com/mail.read",
                    "https://outlook.office.com/mail.readwrite",
                    "https://outlook.office.com/mail.send"
                };

            result = await app.AcquireTokenAsync(scopes);
            Console.WriteLine(result.Token);
        }
        catch (MsalException ex)
        {

        }
    }

here 是向 Windows 桌面应用程序添加登录的详细示例。