Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法以静默方式获取令牌-Microsoft Graph API_C#_Asp.net Mvc_Token_Microsoft Graph Api - Fatal编程技术网

C# 无法以静默方式获取令牌-Microsoft Graph API

C# 无法以静默方式获取令牌-Microsoft Graph API,c#,asp.net-mvc,token,microsoft-graph-api,C#,Asp.net Mvc,Token,Microsoft Graph Api,我们正在使用Microsoft Graph API开发一个ASP.NET MVC项目。它主要基于上的示例代码。该应用程序一开始运行良好-我们可以使用我们的租户帐户登录并从图表中获取数据。但是,如果我们从VisualStudio启动一个新会话,或者只是等待一段时间,AcquireTokenSilentAsync将抛出 无法以静默方式获取令牌 如果清除web浏览器缓存,它将再次工作,但一段时间后,错误将返回。 我们已经尝试将权限更改为common、Organization和tenant,但错误仍然存

我们正在使用Microsoft Graph API开发一个ASP.NET MVC项目。它主要基于上的示例代码。该应用程序一开始运行良好-我们可以使用我们的租户帐户登录并从图表中获取数据。但是,如果我们从VisualStudio启动一个新会话,或者只是等待一段时间,
AcquireTokenSilentAsync
将抛出

无法以静默方式获取令牌

如果清除web浏览器缓存,它将再次工作,但一段时间后,错误将返回。 我们已经尝试将权限更改为common、Organization和tenant,但错误仍然存在

我们获取访问令牌的方法如下:

//获取访问令牌及其过期日期。首先尝试从令牌缓存中获取令牌。
公共异步任务GetUserAccessTokenAsync()
{
//初始化缓存。
HttpContextBase context=HttpContext.Current.GetOwinContext().Environment[“System.Web.HttpContextBase”]作为HttpContextBase;
tokenCache=新SessionTokenCache(
ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value,
上下文);
IEnumerable cachedItems=tokenCache.ReadItems(appId);//查看缓存中的内容
如果(cachedItems.Count()>0)
返回cachedItems.First().Token;
如果(!redirectUri.EndsWith(“/”)redirectUri=redirectUri+“/”;
string[]segments=context.Request.Path.Split(新字符[]{'/'});
机密客户端应用程序cca=新的机密客户端应用程序(
阿皮德,
重定向URI+段[1],
新客户端凭据(appSecret),
令牌缓存);
字符串allScopes=非管理范围;
字符串[]scopes=allScopes.Split(新字符[]{''});
尝试
{
AuthenticationResult=等待cca.AcquireTokenSilentAsync(作用域);
返回result.Token;
}
//无法以静默方式检索访问令牌。
捕获(MsalSilentTokenAcquisitionException)
{
HttpContext.Current.Request.GetOwinContext().Authentication.Challenge(
新建AuthenticationProperties(){RedirectUri=RedirectUri+segments[1]},
OpenIdConnectAuthenticationDefaults.AuthenticationType);
抛出新的ServiceException(
新错误
{
Code=GraphErrorCode.AuthenticationFailure.ToString(),
Message=Resource.Error\u authChallengeed,
});
}
}

你知道为什么不能以静默方式获取令牌吗?

你如何处理抛出的异常?在您提供的示例中,它们使用try/catch处理错误,如果引发异常且错误消息匹配,则返回空结果。然后OpenId中间件将拦截此请求(因为..GetOwinContext().Authentication.Challenge()将响应代码设置为401,将身份验证类型设置为OpenIdConnectAuthenticationDefaults.AuthenticationType)

        try
        {

            // Initialize the GraphServiceClient.
            GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

            // Get the files and folders in the current user's drive.
            results.Items = await filesService.GetMyFilesAndFolders(graphClient);
        }
        catch (ServiceException se)
        {
            if (se.Error.Message == Resource.Error_AuthChallengeNeeded) return new EmptyResult();
            return RedirectToAction("Index", "Error", new { message = string.Format(Resource.Error_Message, Request.RawUrl, se.Error.Code, se.Error.Message) });
        }