首先,我不确定这是否重要,但是由于@ Simon Mourier him answer 中提到的原因,我使用的是ADAL的EXPERIMENTAL版本,即 this one

在下面的代码中,我想同步检索AuthenticationResult,因此,我将等待AcquireTokenAsync方法以同步方式完成身份验证。

这是因为授权完成后应设置一个 bool 标志(isAuthorized = true),但是tgis需要以同步方式发生,因为如果没有,那么我可以调用该类的其他方法,这些方法将抛出空引用,因为该调用到AcquireTokenAsync的操作尚未完成,因此该对象为null。

以下代码不起作用,该方法将永远不会返回,因为对AcquireTokenAsync方法的调用似乎无限期地卡住了线程。

C#(由于在线翻译,语法可能不正确):

public void Authorize() {
    // Use the 'Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory' Nuget package for auth.
    this.authContext = new AuthenticationContext(this.authUrl, this.cache);
    this.authResult = this.authContext.AcquireTokenAsync({ "https://outlook.office.com/mail.readwrite" },
                                                         null, this.clientIdB, this.redirectUriB,
                                                         new PlatformParameters(PromptBehavior.Auto, this.windowHandleB)).Result;

    // Use the 'Microsoft.Office365.OutlookServices-V2.0' Nuget package from now on.
    this.client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"), () => Task.FromResult(this.authResult.Token));
    this.isAuthorizedB = true;
}

VB.NET:
Public Sub Authorize()

    ' Use the 'Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory' Nuget package for auth.
    Me.authContext = New AuthenticationContext(Me.authUrl, Me.cache)

    Me.authResult =
        Me.authContext.AcquireTokenAsync({"https://outlook.office.com/mail.readwrite"},
                                         Nothing, Me.clientIdB, Me.redirectUriB,
                                         New PlatformParameters(PromptBehavior.Auto, Me.windowHandleB)).Result

    ' Use the 'Microsoft.Office365.OutlookServices-V2.0' Nuget package from now on.
    Me.client = New OutlookServicesClient(New Uri("https://outlook.office.com/api/v2.0"),
                                           Function() Task.FromResult(Me.authResult.Token))

    Me.isAuthorizedB = True

End Sub

我进行了一些研究,尝试了其他两种替代方法,但发生的情况相同。

第一个:
ConfiguredTaskAwaitable<AuthenticationResult> t = this.authContext.AcquireTokenAsync(scopeUrls.ToArray(), null, this.clientIdB, this.redirectUriB, new PlatformParameters(PromptBehavior.Auto, this.windowHandleB)).ConfigureAwait(false);

this.authResult = t.GetAwaiter.GetResult();

第二名:
this.authResult == RunSync(() => { return this.authContext.AcquireTokenAsync(scopeUrls.ToArray(), null, this.clientIdB, this.redirectUriB, new PlatformParameters(PromptBehavior.Auto, this.windowHandleB)); })

private AuthenticationResult RunSync<AuthenticationResult>(Func<Task<AuthenticationResult>> func)
{
    return Task.Run(func).Result;
}

最佳答案



我怀疑此问题是由在UI线程中调用async方法引起的。当前,我的解决方法是将调用包装为新的工作线程。

    private void button1_Click(object sender, EventArgs e)
    {
        Authorize().Wait();
    }

    private Task Authorize()
    {
        return Task.Run(async () => {
            var authContext = new AuthenticationContext("https://login.microsoftonline.com/common");

            var authResult = await authContext.AcquireTokenAsync
            (new string[] { "https://outlook.office.com/mail.readwrite" },
             null,
             "{client_id}",
             new Uri("urn:ietf:wg:oauth:2.0:oob"),
             new PlatformParameters(PromptBehavior.Auto, null));
        });
    }

关于c# - 如何同步等待 'AuthenticationContext.AcquireTokenAsync()'?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37366991/

10-13 00:08