ADAL令牌不刷新

时间:2015-09-21 17:49:32

标签: asp.net vb.net webforms azure-active-directory adal

我有一个ASP.NET webforms应用程序,其中我使用Azure Key Vault与Azure Active目录相关联。我使用了https://azure.microsoft.com/en-us/documentation/articles/storage-encrypt-decrypt-blobs-key-vault/中的指南  从Azure Active Directory获取我的应用程序的令牌并使用它来访问我的密钥库,我最终将其用于存储加密。应用程序第一次请求令牌时,一切都运行良好,但令牌过期后(一小时后)。应用程序不会检索新的令牌。我正在使用最新的稳定版本Microsoft.IdentityModel.Clients.ActiveDirectory 2.19.208020213,并且也尝试了最新的预发行版(3.5.208051316-alpha)。

GetToken方法如下所示

 Public Async Function GetToken(authority As String, resource As String, scope As String) As Task(Of String)
    Dim authContext = New AuthenticationContext(authority)
    Dim clientCred As New ClientCredential(CloudConfigurationManager.GetSetting("ClientID"), CloudConfigurationManager.GetSetting("ClientSecret"))
    System.Diagnostics.Trace.TraceInformation("Attempting to acquire auth token")
    Dim result As AuthenticationResult = await authContext.AcquireTokenAsync(resource, clientCred)
   System.Diagnostics.Trace.TraceInformation("Auth returned")
    If result Is Nothing Then
        System.Diagnostics.Trace.TraceInformation("Auth was null")
        Throw New InvalidOperationException("Failed to obtain the JWT token")
    End If
     System.Diagnostics.Trace.TraceInformation("Returning auth access token")
    Return result.AccessToken
End Function

此处用于获取与密钥库的连接

Dim cloudResolver As New KeyVaultKeyResolver(AddressOf GetToken)

GetToken方法只挂起在AcquireTokenAsync上。我在ADAL中打开了详细日志记录,这就是日志显示的内容,它停止了,GetToken永远不会返回。

-Application: 2015-09-21T17:12:13  PID[8884] Information 9/21/2015 5:12:13 PM: 19ce5dc3-d618-48e9-8bbd-c5b3ad31bfc2 - TokenCache: Looking up cache for a token...
-Application: 2015-09-21T17:12:13  PID[8884] Information 9/21/2015 5:12:13 PM: 19ce5dc3-d618-48e9-8bbd-c5b3ad31bfc2 - TokenCache: An item matching the requested resource was found in the cache
-Application: 2015-09-21T17:12:13  PID[8884] Information 9/21/2015 5:12:13 PM: 19ce5dc3-d618-48e9-8bbd-c5b3ad31bfc2 - TokenCache: An expired or near expiry token was found in the cache
-Application: 2015-09-21T17:12:13  PID[8884] Information 9/21/2015 5:12:13 PM: 19ce5dc3-d618-48e9-8bbd-c5b3ad31bfc2 - TokenCache: An old item was removed from the cache

此外,我尝试通过将令牌缓存设置为Nothing来关闭令牌缓存,然后ADAL甚至不会在第一次检索访问令牌。

1 个答案:

答案 0 :(得分:2)

我在类似的问题Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously

中找到了答案

关键是要删除其中任何一个并用等待

替换它们
.GetAwaiter().GetResult()

例如,这是原始的

Dim theKey = cloudResolver.ResolveKeyAsync($"{CloudConfigurationManager.GetSetting("KeyVaultUrl")}Secret/", CancellationToken.None).GetAwaiter().GetResult()

已被

取代
Dim theKey = await cloudResolver.ResolveKeyAsync($"{CloudConfigurationManager.GetSetting("KeyVaultUrl")}Secret/", CancellationToken.None)