简体   繁体   中英

'IDataProtectionBuilder' does not contain a definition for 'ProtectKeysWithAzureKeyVault' MVC dotnet core 3.1

I am getting an error that ProtectKeysWithAzureKeyVault can't be added to IDataProtectionBuilder in an MVC Startup.cs dotnet 3.1 class

Microsoft.Extensions.Configuration.AzureKeyVault is referenced and the PersistKeysToAzureBlobStorage is working fine, but I would like to use an Azure Key Vault to encrypt the keys at rest.

According to the Microsoft documents this code should work

public void ConfigureServices(IServiceCollection services)
{
    services.AddDataProtection()
        .PersistKeysToAzureBlobStorage(new Uri("<blobUriWithSasToken>"))
        .ProtectKeysWithAzureKeyVault("<keyIdentifier>", "<clientId>", "<clientSecret>");
}

Here's my code

using Microsoft.AspNetCore.DataProtection;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage;

public void ConfigureServices(IServiceCollection services)
{
        if (CloudStorageAccount.TryParse(Configuration["AzureDataProtection:StorageConnectionString"], out CloudStorageAccount storageAccount))
        {
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = cloudBlobClient.GetContainerReference(Configuration["AzureDataProtection:ContainerName"]);
            container.CreateIfNotExistsAsync();
            services.AddDataProtection()
            .PersistKeysToAzureBlobStorage(container, $"{Configuration["AzureDataProtection:ApplicationName"]}{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}keys.xml")
            .ProtectKeysWithAzureKeyVault(Configuration["AzureDataProtection:DataProtectionKey"], Environment.GetEnvironmentVariable("AZURE_CLIENT_ID"), Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET"));
            .SetApplicationName(Configuration["AzureDataProtection:ApplicationName"]);
        }
}

I've been working on the assumption I have missed a package but can't work out what- here's the entire project file.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <UserSecretsId>ed790662-005d-4160-802c-4900c2d6daf0</UserSecretsId>
    <GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="Styles\ovbootstrap.css" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Google.Apis.Auth" Version="1.43.0" />
    <PackageReference Include="Google.Apis.Drive.v3" Version="1.43.0.1835" />
    <PackageReference Include="Google.Apis.Sheets.v4" Version="1.43.0.1848" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureStorage" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="RestSharp" Version="106.10.1" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Temp\" />
    <Folder Include="Views\Shared\Components\MeasurerSurveyorFitterFilterMenu\" />
    <Folder Include="Views\Shared\Components\ExportHeaderSummary\" />
    <Folder Include="wwwroot\css\" />
    <Folder Include="wwwroot\js\" />
    <Folder Include="wwwroot\lib\" />
    <Folder Include="wwwroot\images\" />
  </ItemGroup>
</Project>

But I get this error: CS1061 'IDataProtectionBuilder' does not contain a definition for 'ProtectKeysWithAzureKeyVault' and no accessible extension method 'ProtectKeysWithAzureKeyVault' accepting a first argument of type 'IDataProtectionBuilder' could be found (are you missing a using directive or an assembly reference?)

I've tried referencing Microsoft.AspNetCore.DataProtection.AzureStorage as well as through cleaning / rebuilding.

Any suggestions greatly appreciated!

Looks like you are lacking the Key Vault package https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.AzureKeyVault/ .

I found this out by googling the name, which lead me to the assembly name where this function is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM