Asp.net mvc 无法从global.asax检索Azure graph令牌

Asp.net mvc 无法从global.asax检索Azure graph令牌,asp.net-mvc,azure-active-directory,microsoft-graph-api,azure-ad-graph-api,global-asax,Asp.net Mvc,Azure Active Directory,Microsoft Graph Api,Azure Ad Graph Api,Global Asax,我正在处理MVC应用程序,我能够从控制器获取图形令牌,但同一代码(PFB代码)在Global.asax.cs中不起作用 它没有给出任何错误,但在authContext.AcquireTokenAsync处暂停,并且没有向前移动 protected void Session_Start(Object sender, EventArgs e) { string tenantId = "TenantId"; string clientId

我正在处理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;
        }
谁能帮我一下吗


因为
等待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;
}

Ha它永远不会执行@Avinash它的
Void
方法。你的代码似乎没问题。尝试使用另一个具有异步方法的cs文件,希望能奏效。请注意,async void是“fire and forget”因此,在方法返回后,将在有点奇怪的状态下运行continuation,因为调用此函数的框架组件不会捕获抛出的异常。我认为不需要
async
,因为在示例代码中,您只是调用
。Result
,它将一直阻塞到完成。但由于代码不匹配,因此需要在屏幕截图中显示