gpt4 book ai didi

c# - 如何同步等待 'AuthenticationContext.AcquireTokenAsync()'?

转载 作者:太空狗 更新时间:2023-10-29 20:52:42 24 4
gpt4 key购买 nike

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


在下面的代码中,我想同步获取一个AuthenticationResult,所以,我将以同步方式等待AcquireTokenAsync方法完成身份验证。

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

下面的代码不起作用,该方法永远不会返回,因为对 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;
}

最佳答案

How to wait for 'AuthenticationContext.AcquireTokenAsync()' synchronouslly?

我怀疑这个问题是由于在 UI 线程中调用异步方法引起的。目前,我的解决方法是将调用封装到一个新的工作线程中。

    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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com