用于从 Azure AD B2C 回调的操作 ID,用于日志记录



我正在尝试确保我的日志看起来正确,但从 Azure AD B2C 获取访问令牌所需的池服务存在一些问题。

问题是AD用来再次访问我的系统的回调函数。由于操作 ID 不正确,因此未正确记录从回调完成的所有调用。有没有办法将操作 ID 传递到 AD 世界中?

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

根据您的问题,池服务在需要获取访问令牌时会导致问题。实际上,我还没有看到我们可以将 operationId 或任何特定 ID 传递给 Azure AD 的方法。现在,可以使用 MSAL 库通过 AcquireTokenByUsernamePasswordAsync(( 方法从 Azure AD B2C 获取访问令牌,并使用自定义操作 ID 记录它。

有关使用用户名/密码获取令牌的更多信息,请查看以下文档链接:

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

我希望这有所帮助。

最新更新