azure.net-coreazure-blob-storageazure-keyvaultweb-farm

AddDataProtection().PersistKeysToAzureBlobStorage().ProtectKeysWithAzureKeyVault() getting httpVerb Error while accessing the blob storage


Application is hosted in Web Farm Scenario. I dont have an option to persist keys for dataprotection() in Database. I am not able to Persist the Key file in blob storage.

I have referred documentation and used these lines of code to store keyfile in blob.

builder.Services.AddDataProtection().PersistKeysToAzureBlobStorage('blob-sas-uri')
.ProtectKeysWithAzureKeyVault('keyvault identifier')

When i run the application I am getting the following error.

RequestFailedException: The resource doesn't support specified Http Verb.
 ErrorCode: UnsupportedHttpVerb Content:
Azure.Storage.Blobs.BlockBlobRestClient.Upload(long contentLength, Stream body, Nullable<int> timeout, byte[] transactionalContentMD5, string blobContentType, string blobContentEncoding, string blobContentLanguage,...

Am I missing any configuration in Azure portal ?

Thanks in Advance.


Solution

  • code to store keyfile in blob.

    As an alternative to SAS url , I have given RBAC role Storage Contributor and KeyVault Crypto User role, I used below code and it works for me:

    Program.cs:

    rith_b.Services.AddAzureClients(ri_cb =>
    {
        ri_cb.AddBlobServiceClient(new Uri("https://rithwik.blob.core.windows.net"));
    });
    rith_b.Services.AddDataProtection()
        .PersistKeysToAzureBlobStorage(new Uri("https://rithwik.blob.core.windows.net/rithwik/test.xml"), new DefaultAzureCredential())
        .ProtectKeysWithAzureKeyVault(new Uri("https://rith98.vault.azure.net/keys/test"), new DefaultAzureCredential());
    

    Key in KeyVault:

    enter image description here

    Output:

    Blob got created:

    enter image description here

    Edit:

    You can use App registration and create service principal to which you have to give the needed roles:

    For Cloud you can use:

    using Azure.Identity;
    -----
    -----
    string clntId = "xxx";
    string clntSecret = "xxx";
    string tenantId = "xxx";
    var r_cred = new ClientSecretCredential(tenantId, clntId, clntSecret);
    rith_b.Services.AddAzureClients(ri_cb =>
    {
        ri_cb.AddBlobServiceClient(new Uri("https://rithwik.blob.core.windows.net"))
                     .WithCredential(r_cred);
    });
    rith_b.Services.AddDataProtection()
        .PersistKeysToAzureBlobStorage(new Uri("https://stgname.blob.core.windows.net/rithwik/test.xml"), r_cred)
        .ProtectKeysWithAzureKeyVault(new Uri("https://keyvaultname.vault.azure.net/keys/test"), r_cred);
    -----
    -----