该行永远挂起:authContext.AcquireTokenAsync("https://graph.windows.net",凭据); [英] This line hangs forever: authContext.AcquireTokenAsync("https://graph.windows.net", credential);

查看:410
本文介绍了该行永远挂起:authContext.AcquireTokenAsync("https://graph.windows.net",凭据);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:当我使用Microsoft的项目时,标题中提到的代码行可以正确运行并进行身份验证,并且可以对用户执行操作,如"B2CGraphClient"示例项目中所示. 但是,当我将B2CGraphClient.cs复制并粘贴到Web应用程序中时,此行代码将永远挂起.怎么会这样?

My issue is: When I use Microsoft's project, the line of code mentioned in the title runs and authenticates correctly, and I can perform operations on users as demonstrated in the "B2CGraphClient" sample project. However when I copy-and-paste B2CGraphClient.cs into my web application, this line of code hangs forever. How can this be?

B2CGraphClient.cs中,挂起的行是#184.

The line that hangs is #184 in B2CGraphClient.cs.

详细信息: 我正在使用此此处.这些文件需要设置变量clientIdclientSecrettenant,这些变量我可以正确设置以适合我的AAD B2C实例.当我将B2CGraphClient.cs代码复制到我的Web应用程序项目中时,这些变量的值也已正确设置,所以我认为这不是问题.

Details: I am using the sample project named "B2CGraphClient" mentioned in this article, whose zipfile is located here. The files require setting the variables clientId, clientSecret, and tenant, which I was able to set correctly to fit my AAD B2C instance. The values of these variables were also correctly set when I copied the B2CGraphClient.cs code into my web app project, so I don't think that is the issue.

线索: 这些观察结果可能是问题所在:

Clues: These observations could potentially be the issues:

  1. 运行Microsoft示例"B2CGraphClient"代码时,它不需要用户认证到Web应用程序中;但是,我的Web应用程序确实要求用户在使用Graph Client之前输入其用户名/密码.
  2. 在我的Web应用程序中运行标题中的代码行时,以下是浏览器中的URL:https://login.microsoftonline.com/<my_domain>.onmicrosoft.com/B2C_1_SiUpIn/......我知道B2C_1_SiUpIn策略名称不应该在此URL中.但是我该如何解决呢?
  1. When running the Microsoft sample "B2CGraphClient" code, it does not require that a user authenticates into a web application; however, my web application does require a user enter his/her username/password before use of the Graph Client.
  2. When the line of code in the title is run in my web application, the following is the URL in the browser: https://login.microsoftonline.com/<my_domain>.onmicrosoft.com/B2C_1_SiUpIn/...... I know that the B2C_1_SiUpIn policy name is not supposed to be in this URL. But how do I fix this?

谢谢!

更新

我正在发布初始化B2CGraphClient的代码,该代码显示(至少在我看来是这样)传递给客户端以创建其凭据的唯一信息是clientIdclientSecrettenant名称.

I am posting the code which initializes the B2CGraphClient, which shows that (at least, it looks to me) the only pieces of information that are being passed into the client in order to create its credentials are the clientId, clientSecret, and tenant name.

    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稍后用于验证图形客户端:

The credential is later used to authenticate the graph client:

        AuthenticationResult result = await authContext.AcquireTokenAsync(aadGraphResourceId, credential);

推荐答案

B2CGraphClient已针对控制台应用程序开发.

B2CGraphClient has been developed for a console application.

要将B2CGraphClient集成到Web应用程序中,应将AcquireToken调用从同步方法更改为异步方法.

To integrate B2CGraphClient in to a web application, you should change the AcquireToken calls from the synchronous method to the asynchronous method.

例如更改自:

AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net", credential);

收件人:

AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.windows.net", credential);

这篇关于该行永远挂起:authContext.AcquireTokenAsync("https://graph.windows.net",凭据);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆