locked
KeyVaultTokenCallback usage VB.NET RRS feed

  • Question

  • Im trying to convert this to VB.NET but can't seem to get the syntax right. VS is complaining about the KeyVaultTokenCallback where it says it needs an "AddressOf" to method or lambda expression. Unfortunately, the web link doesn't provide a VB.NET example. Any help is appreciated.

    This example comes from https://docs.microsoft.com/en-us/azure/key-vault/general/service-to-service-authentication

    using Microsoft.Azure.Services.AppAuthentication;
    using Microsoft.Azure.KeyVault;
    
    // Instantiate a new KeyVaultClient object, with an access token to Key Vault
    var azureServiceTokenProvider1 = new AzureServiceTokenProvider();
    var kv = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider1.KeyVaultTokenCallback));
    

    Wednesday, September 2, 2020 5:31 PM

Answers

  • I wound up doing something in C# that seemed to work better. Still getting used to Azure security with API calls. Still need to figure this out in VB for old apps that I maintain. They should be retired and the code base changed into more modern code.
    Tuesday, September 8, 2020 10:05 AM

All replies

  • Did you try to write AddressOf, like this: Dim kv = New KeyVaultClient( New KeyVaultClient.AuthenticationCallback( AddressOf azureServiceTokenProvider1.KeyVaultTokenCallback))?

    Wednesday, September 2, 2020 7:13 PM
  • I tried that but it still didn't like it.
    Wednesday, September 2, 2020 7:16 PM
  • It seems that the delegates, which appear in this code, are currently different. (Maybe the libraries were updated). Try this:

    Dim kv = New KeyVaultClient(Function(authority, resource, scope) azureServiceTokenProvider1.KeyVaultTokenCallback(authority, resource, scope))

     




    • Edited by Viorel_MVP Wednesday, September 2, 2020 7:36 PM
    Wednesday, September 2, 2020 7:35 PM
  • What about something like this?
            Dim keyVaultClient As New KeyVaultClient(New AuthenticationCallback(Function() As Task(Of String)
                                                                                    Return New AzureServiceTokenProvider().KeyVaultTokenCallback.Target
                                                                                End Function))

    Wednesday, September 2, 2020 7:42 PM
  • What about something like this?
            Dim keyVaultClient As New KeyVaultClient(New AuthenticationCallback(Function() As Task(Of String)
                                                                                    Return New AzureServiceTokenProvider().KeyVaultTokenCallback.Target
                                                                                End Function))


    If it compiles, but does not work, then add Option Strict On (first line of the file) and check if it works.


    Wednesday, September 2, 2020 7:47 PM
  • Option strict on gives an error

    Disallows implicit conversions from Object to Task(Of string)

    ... now what? 

     
    Wednesday, September 2, 2020 8:06 PM
  • Hi George Hendrickson,

    You can use GetAccessTokenAsync instead of the azureServiceTokenProvider to get the access token.

            Dim keyVaultClient As KeyVaultClient = New KeyVaultClient(New KeyVaultClient.AuthenticationCallback(AddressOf GetAccessTokenAsync))
    
    Public Async Function GetAccessTokenAsync() As Task(Of String)
            Dim azureServiceTokenProvider = New AzureServiceTokenProvider()
            Dim accessToken As String = Await azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net")
            Return accessToken
        End Function

    For more details you can check the following reference:

    How to retrieve secret from Azure Key Vault

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 3, 2020 7:14 AM
  • Option strict on gives an error

    Disallows implicit conversions from Object to Task(Of string)

    ... now what? 

     

    Use the code that does not have compilation errors, and which works.


    Thursday, September 3, 2020 8:07 AM
  • With Option Strict On, I get the error

    "Option Strict On does not allow narrowing in implicit type conversions between 'Public Function GetAccessTokenAsync() as Task(Of String) and 'Delegate Function KeyVaultClient.AuthenticationCallback(authority As String, resource as String, scope as String) as Task(Of String)


    Thursday, September 3, 2020 1:07 PM
  • Hi George Hendrickson,

    To eliminate this error, you can consider adding some necessary parameters to the 'GetAccessTokenAsync' method.

        Public Async Function GetAccessTokenAsync(authority As String, resource As String, scope As String) As Task(Of String)
            Dim azureServiceTokenProvider = New AzureServiceTokenProvider()
            Dim accessToken As String = Await azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net")
            Return accessToken
        End Function

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 4, 2020 2:19 AM
  • Hi George Hendrickson,

    How is the question going? Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 8, 2020 1:34 AM
  • I wound up doing something in C# that seemed to work better. Still getting used to Azure security with API calls. Still need to figure this out in VB for old apps that I maintain. They should be retired and the code base changed into more modern code.
    Tuesday, September 8, 2020 10:05 AM
  • Hi George Hendrickson,

    You can submit your feedback on MicrosoftDocs/azure-docs.

    Best Regards,

    Xingyu Zhao

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 9, 2020 1:42 AM
  • Hi George Hendrickson,

    If you have any questions about azure key vault in the future, you can consider posting the question in azure-key-vault(Q&A) forum for better responses. Thanks.

    Best Regards,

    Xingyu Zhao 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 15, 2020 7:23 AM