我正在尝试确保我的日志看起来正确,但是从azure ad b2c获取访问令牌所需的池服务存在一些问题。
问题是AD用来再次访问我的系统的回调函数。由于operationID不正确,所以从回调中完成的所有调用都未正确记录。有没有办法将OperationId传递到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进行记录。
有关使用用户名/密码获取令牌的更多信息,请查看以下文档链接:
我希望这有帮助。