这行永远挂起:authContext.AcquireTokenAsync("https://graph.windows.net", credential);
This line hangs forever: authContext.AcquireTokenAsync("https://graph.windows.net", credential);
我的问题是:当我使用微软的项目时,标题中提到的代码行运行s并且验证正确,我可以对用户进行操作如 "B2CGraphClient" 示例项目所示。 然而 当我 copy-and-paste B2CGraphClient.cs
进入我的网络应用程序时,这行代码永远挂起。怎么会这样?
挂起的行是B2CGraphClient.cs
中的#184。
详情:
我正在使用 article, whose zipfile is located here 中提到的名为 "B2CGraphClient" 的示例项目。这些文件需要设置变量 clientId
、clientSecret
和 tenant
,我能够正确设置这些变量以适合我的 AAD B2C 实例。当我将 B2CGraphClient.cs
代码复制到我的 Web 应用程序项目中时,这些变量的值也已正确设置,所以我认为这不是问题所在。
线索:
这些观察结果可能是问题所在:
- 运行使用 Microsoft 示例 "B2CGraphClient" 代码时,它不需要用户对 Web 应用程序进行身份验证;但是,我的 Web 应用程序确实需要用户在使用 Graph 客户端之前输入 his/her username/password。
- 当标题中的代码行在我的网络应用程序中为运行时,以下是浏览器中的URL:
https://login.microsoftonline.com/<my_domain>.onmicrosoft.com/B2C_1_SiUpIn/......
我知道B2C_1_SiUpIn
策略名称不应在此 URL 中。但是我该如何解决这个问题?
谢谢!
更新
我正在发布初始化 B2CGraphClient 的代码,它表明(至少,在我看来)唯一被传递到客户端以创建其凭据的信息是 clientId
、clientSecret
和 tenant
姓名。
public B2CGraphClient(string clientId, string clientSecret, string tenant)
{
// The client_id, client_secret, and tenant are pulled in from the App.config file
this.clientId = clientId;
this.clientSecret = clientSecret;
this.tenant = tenant;
// The AuthenticationContext is ADAL's primary class, in which you indicate the direcotry to use.
this.authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
// The ClientCredential is where you pass in your client_id and client_secret, which are
// provided to Azure AD in order to receive an access_token using the app's identity.
this.credential = new ClientCredential(clientId, clientSecret);
}
credential
稍后用于验证图形客户端:
AuthenticationResult result = await authContext.AcquireTokenAsync(aadGraphResourceId, credential);
B2CGraphClient
已为控制台应用程序开发。
要将 B2CGraphClient
集成到 Web 应用程序中,您应该将 AcquireToken
调用从同步方法更改为异步方法。
例如更改自:
AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net", credential);
至:
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.windows.net", credential);
我的问题是:当我使用微软的项目时,标题中提到的代码行运行s并且验证正确,我可以对用户进行操作如 "B2CGraphClient" 示例项目所示。 然而 当我 copy-and-paste B2CGraphClient.cs
进入我的网络应用程序时,这行代码永远挂起。怎么会这样?
挂起的行是B2CGraphClient.cs
中的#184。
详情:
我正在使用 article, whose zipfile is located here 中提到的名为 "B2CGraphClient" 的示例项目。这些文件需要设置变量 clientId
、clientSecret
和 tenant
,我能够正确设置这些变量以适合我的 AAD B2C 实例。当我将 B2CGraphClient.cs
代码复制到我的 Web 应用程序项目中时,这些变量的值也已正确设置,所以我认为这不是问题所在。
线索: 这些观察结果可能是问题所在:
- 运行使用 Microsoft 示例 "B2CGraphClient" 代码时,它不需要用户对 Web 应用程序进行身份验证;但是,我的 Web 应用程序确实需要用户在使用 Graph 客户端之前输入 his/her username/password。
- 当标题中的代码行在我的网络应用程序中为运行时,以下是浏览器中的URL:
https://login.microsoftonline.com/<my_domain>.onmicrosoft.com/B2C_1_SiUpIn/......
我知道B2C_1_SiUpIn
策略名称不应在此 URL 中。但是我该如何解决这个问题?
谢谢!
更新
我正在发布初始化 B2CGraphClient 的代码,它表明(至少,在我看来)唯一被传递到客户端以创建其凭据的信息是 clientId
、clientSecret
和 tenant
姓名。
public B2CGraphClient(string clientId, string clientSecret, string tenant)
{
// The client_id, client_secret, and tenant are pulled in from the App.config file
this.clientId = clientId;
this.clientSecret = clientSecret;
this.tenant = tenant;
// The AuthenticationContext is ADAL's primary class, in which you indicate the direcotry to use.
this.authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
// The ClientCredential is where you pass in your client_id and client_secret, which are
// provided to Azure AD in order to receive an access_token using the app's identity.
this.credential = new ClientCredential(clientId, clientSecret);
}
credential
稍后用于验证图形客户端:
AuthenticationResult result = await authContext.AcquireTokenAsync(aadGraphResourceId, credential);
B2CGraphClient
已为控制台应用程序开发。
要将 B2CGraphClient
集成到 Web 应用程序中,您应该将 AcquireToken
调用从同步方法更改为异步方法。
例如更改自:
AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net", credential);
至:
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.windows.net", credential);