【问题标题】:'IDataProtectionBuilder' does not contain a definition for 'ProtectKeysWithAzureKeyVault' MVC dotnet core 3.1“IDataProtectionBuilder”不包含“ProtectKeysWithAzureKeyVault”MVC dotnet core 3.1 的定义
【发布时间】:2020-05-18 03:42:49
【问题描述】:

我收到一个错误,即无法将 ProtectKeysWithAzureKeyVault 添加到 MVC Startup.cs dotnet 3.1 类中的 IDataProtectionBuilder

Microsoft.Extensions.Configuration.AzureKeyVault 被引用并且 PersistKeysToAzureBlobStorage 工作正常,但我想使用 Azure Key Vault 来加密静态密钥。

根据Microsoft documents,这段代码应该可以工作

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

这是我的代码

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"]);
        }
}

我一直在假设我错过了一个包但无法弄清楚 - 这是整个项目文件。

<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>

但我收到此错误:CS1061“IDataProtectionBuilder”不包含“ProtectKeysWithAzureKeyVault”的定义,并且找不到接受“IDataProtectionBuilder”类型的第一个参数的可访问扩展方法“ProtectKeysWithAzureKeyVault”(您是否缺少 using 指令或程序集参考?)

我尝试过引用 Microsoft.AspNetCore.DataProtection.AzureStorage 以及通过清理/重建。

任何建议都非常感谢!

【问题讨论】:

  • .SetApplicationName 之前有一个分号——您确定这正是您的代码吗? Microsoft.AspNetCore.DataProtection 有用法吗? PersistKeysToAzureBlobStorage 有效,但 ProtectKeysWithAzureKeyVault 无效?
  • 分号分开,是的!我已将 ProtectKeysWithAzureKeyVault 注释掉,因此它可以编译并将分号向上移动。是的,PersistKeysToAzureBlobStorage 有效,但 ProtectKeysWithAzureKeyVault 无效

标签: c# azure asp.net-core


【解决方案1】:

您似乎缺少 Key Vault 包 https://www.nuget.org/packages/Microsoft.AspNetCore.DataProtection.AzureKeyVault/

我通过谷歌搜索名称发现了这一点,这使我找到了定义此函数的程序集名称。

【讨论】:

  • 谢谢!为了我将来的使用,感谢您解释您是如何解决的!
  • 在我的情况下,我需要安装 AzureKeyVault 包以及 Microsoft.AspNetCore.DataProtection.AzureStorage,以防其他人被卡住
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
相关资源
最近更新 更多