2015-09-21 129 views
2

我有一个ASP.NET webforms应用程序,我在其中使用与Azure Active Directory关联的Azure Key Vault。我已使用在此处找到的指南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)。ADAL令牌不刷新

的方法为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甚至不会检索访问令牌中的第一次。

回答

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)