无法从 global.asax 检索 Azure 图形令牌

Not able retrieve Azure graph token from global.asax

我正在处理 MVC 应用程序,我能够从控制器获取图形标记,但相同的代码(PFB 代码)在 Global.asax.cs

中不起作用

它没有给出任何错误,但它在 authContext.AcquireTokenAsync 处暂停并且没有继续前进。

protected void Session_Start(Object sender, EventArgs e)
        {
            string tenantId = "TenantId";
            string clientId = "CleintId";
            string clientSecret = "ClientSecret";
            AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
            ClientCredential credential = new ClientCredential(clientId, clientSecret);
            AuthenticationResult result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", credential);
            return result.AccessToken;
        }

谁能帮帮我。

因为await authContext.AcquireTokenAsync是一个异步方法,所以它会等待,直到它完成后才会继续。

Global.asax.cs中使用如下代码:

protected async void Session_Start(Object sender, EventArgs e)
{
    string tenantId = "xxxxxxxxxxxxxxxxxxxxxx";
    string clientId = "xxxxxxxxxxxxxxxxxxxxxx";
    string clientSecret = "xxxxxxxxxxxxxxxxxxxxxx";
    AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId);
    ClientCredential credential = new ClientCredential(clientId, clientSecret);
    AuthenticationResult result =authContext.AcquireTokenAsync("https://graph.microsoft.com", credential);
    var accesstoken = result.AccessToken;
}