-->

This line hangs forever: authContext.AcquireTokenA

2019-08-02 21:43发布

问题:

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?

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

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. 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?

Thank you!


UPDATE

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);
    }

The credential is later used to authenticate the graph client:

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

回答1:

B2CGraphClient has been developed for a console application.

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

E.g. Change from:

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

to:

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