ADAL.Net TokenCache抛出服务器超时错误,错误代码为500 [英] ADAL.Net TokenCache throwing server timeout error with 500 error code

查看:171
本文介绍了ADAL.Net TokenCache抛出服务器超时错误,错误代码为500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OWIN将WS-Federation集成到我的Asp.Net MVC应用程序中。我遵循了github示例,并且按预期运行。

I am trying to integrate WS-Federation in my Asp.Net MVC app using OWIN. I followed the github samples and it is working as expected.

现在,我想更进一步,并从我的网站中调用托管在不同Azure Web应用程序上的外部WebApi。在这种情况下,我找不到任何WS-Fed示例。 WebApi需要访问令牌来提供对受保护资源的访问。在我的一个MVC控制器中,我尝试使用ADAL.Net代码获取访问令牌,但出现超时错误。

Now I want to take this one step further and call an external WebApi hosted on different Azure web app from within my website. I couldn't find any WS-Fed samples for this scenario. WebApi needs an access token to provide access to protected resources. In one of my MVC controllers I tried using ADAL.Net code to acquire the access token but i get timeout error.

string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;    
var authContext1 = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority, new NaiveSessionCache(userObjectID));
    var credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, appKey);
    var tokenResult = await authContext1.AcquireTokenAsync(todoListResourceId, credential);

我从azure的其中一个复制了NaiveSessionCache 样本
然后,我使用HttpClient将此tokenResult.AccessToken传递给webApi调用。这给了我500个服务器超时错误。

I copied NaiveSessionCache from one of the azure samples on github. Then I pass this tokenResult.AccessToken to webApi call using HttpClient. This gives me 500 server timeout error.

但是,如果我不在authContext1中使用NaiveSessionCache并在其构造函数中将其替换为false(无缓存),则代码可以正常工作。

However, if I don't use NaiveSessionCache in authContext1 and replace it with false in its constructor (no cache), code works fine.

我在这里想念什么?谢谢!

What am I missing here? Thanks!

推荐答案

该问题似乎与ADAL的特定版本有关。我可以使用最新版本的ADAL( 3.13.8 )重现此问题。您正在开发的ADAL是什么版本?

The issue seems to relative to the specific version of ADAL. I am able to reproduce this issue using the latest version of ADAL(3.13.8). What's the version of ADAL you were developing?

但是,当我将ADAL降级到 3.9.304210845 (从代码示例中,使用 NaiveSessionCache 类),代码对我来说效果很好。

However when I downgrade the ADAL to the version 3.9.304210845(from the code sample using the NaiveSessionCache class), the code works well for me.

您可以通过以下步骤引用此特定版本:

You can refer this specific version by following steps below:


  1. 在项目文件中替换引用ADAL(*。csproj)的版本

 <Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.9.304210845\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.Platform, Version=3.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.9.304210845\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll</HintPath>
      <Private>True</Private>
    </Reference>




  1. 替换软件包中有关ADAL的版本信息。 config

<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.9.304210845" targetFramework="net45" />




  1. 删除软件包文件夹

重建解决方案以恢复 3.9.304210845 版本ADAL

Rebuild the solution to restore the 3.9.304210845 version ADAL

请让我看看它是否对您有用。为了解决此问题,我建议您从此处

Please let me whether it works for you. And to fix the issue, I suggest that you reopen a new issue from here.

调查后,高级版本的ADAL使用 Task.ConfigureAwait(false)会破坏线程的原始上下文。然后,在使用ADAL获取令牌后,我们将无法访问用于存储令牌的 HttpContext.Current.Session

After the investigation, the high version of ADAL acquire the token using Task.ConfigureAwait(false) which will break the original context of thread. Then we are not able to access the HttpContext.Current.Session which used to store the token after using the ADAL to acquire the token.

这篇关于ADAL.Net TokenCache抛出服务器超时错误,错误代码为500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆