我有一个ASP。. NET webforms应用程序,其中我使用Azure密钥库与Azure活动目录关联。我使用了在这里找到的指南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甚至不会在第一次检索访问令牌。
我在这个类似的问题中找到了答案Azure KeyVault Active Directory AcquireTokenAsync超时时异步调用
关键是要删除其中的任何一个,并用await
代替它们.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)