- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
首先,我不确定这是否重要,但由于@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/
我希望能够以编程方式从 Azure 获取 token 。 我调用 GetAToken().Wait(); 但失败了。 方法是: public async Task GetAToken() {
使用 ADAL 库获取 WAAD token ,我想知道如何更好地控制登录流程。 var ac = new AuthenticationContext("https://login.windows.n
我正在尝试使用 Microsoft Power BI REST API,但由于我对该 API 的经验不是很丰富,所以没有取得多大进展 据我了解,身份验证上下文是身份模型事件目录的一部分,我会三次检查它
首先,我不确定这是否重要,但由于@Simon Mourier 在 him answer 中提到的原因,我正在使用 ADAL 的实验版本, this one . 在下面的代码中,我想同步获取一个Auth
使用 Azure Active Directory 图形客户端 API,如何配置底层 HttpClient 以使用 HttpClientHander,在其中我可以定义经过身份验证的应用程序代理? va
使用 Azure Active Directory 图形客户端 API,如何配置底层 HttpClient 以使用 HttpClientHander,在其中我可以定义经过身份验证的应用程序代理? va
我有一个在 Multi-Tenancy 场景中使用的 native 应用程序。为了对用户进行身份验证——并征得他们同意允许此应用程序代表他们访问 Azure——我只需实例化一个 Authenticat
我正在构建一个 WPF 应用程序,并且现在每次启动时都使用 ADAL 中的 AuthenticationContext,并且希望以某种方式缓存访问 token ,而不必在每次应用程序启动时提示用户。
ADAL library for Windows Phone 8.1 的实现示例可以在 GitHub 上找到 要为调用AuthenticationContext.AcquireTokenAndCont
我目前正在尝试连接到我的 AD 以接收缓存的用户/ token 或我自己的 Vue.js 应用程序在我的团队应用程序中的新 token 。 到目前为止,我已经创建了一个 Teams 应用程序来获取我的
我正在使用 ADAL.js 获取 Azure 资源的 token 。 为此,我编写了以下代码: var endpoints = {
我想运行 Java 代码以在 Windows 服务器中使用代理读取 Azure KeyVault。 我浏览了很多帖子,但可以找到任何可行的解决方案。主要针对 c#,但我想要针对 Java。我的代码在本
我正在关注 tutorial用于通过 AAD 对 Power BI REST API 进行身份验证。 本教程使用 C# 和 ADAL。它通过以下方式获取 token , AuthenticationC
当我从 Windows 应用商店应用 HTML/Javascript 调用 AuthenticationContext 方法时,出现错误。 代码如下: var adal = Microsoft.Ide
我使用 Angular1 构建了一个小型测试应用程序,该应用程序使用 ADAL 连接到 Office365 租户。由于我希望开始包含 Angular2 组件,因此我尝试使用 JSPM 和 System
如果我错过了这篇文章中的某些内容,我深表歉意,因为我在阅读了几个小时后已经束手无策。 我正在尝试编写一个后端服务 (Windows),它将通过 Azure AD 连接到 MS Graph API。我正
我正在使用 Azure AD 验证我的单页应用程序 (Angular4),并为此使用 Adal.js。在登录页面上,我单击一个重定向到 Microsoft AAD 的按钮,成功登录后它重定向回应用程序
我是一名优秀的程序员,十分优秀!