简体   繁体   中英

OperationID for callback from Azure AD B2C for the purpose of logging

I am trying to ensure my log is looking correct, but having some issues with a pool service that is required to get an access token from azure ad b2c.

The problem is the callback function which AD uses to access my system again. All the calls done from the callback are not logged correctly, as the operationID is not correct. Is there a way to pass the OperationId into the AD world?

async Task<string> GetAccessToken()
{
    var uri = $"https://login.microsoftonline.com/.........";

    var parameters = new FormUrlEncodedContent(
        new Dictionary<string, string>()
        {
            {"username", B2CConfiguration.Username},
            {"password", B2CConfiguration.Password},
            {"grant_type", B2CConfiguration.GrantType},
            {"scope", B2CConfiguration.Scope},
            {"client_id", B2CConfiguration.ClientId},
            {"response_type", B2CConfiguration.ResponseType}
        });

    using (var request = await _httpClient.PostAsync(uri, parameters))
    {
        var response = await request.Content.ReadAsStringAsync();
        var token = JsonConvert.DeserializeObject<AccessTokenModel>(response).access_token;
        return $"Bearer {token}";
    }
}

async Task DoWorkAsync()
{
    var operationID = Guid.NewGuid().ToString();
    using (_logClient.CreateRequest("Actor Run",operationID ))  // _logClient is a thin wrapper for the applicationInsights client
    {
        var accessToken = await GetAccessToken();       // This call is not listed under "Actor Run" in Application Insights
        var dataSet = await GetSomeData(accessToken);   // This call is logged correctly
        foreach(var data in dataSet)
        {
            await UseDateToDoSomethingOnExternalServer(dataSet, data);  // This call is logged correctly
        } 
    }
}

As per your question, the pool service is causing issue when it is required to get an access token. Actually, I have not seen a way where we can pass operationId or any specific id to Azure Ad. For now, you can use MSAL library to get the access token from Azure AD B2C by using AcquireTokenByUsernamePasswordAsync() method and log it with your custom operation id.

For more information about getting a token with username/password, please check the below documentation link:

https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Username-Password-Authentication

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM