.NET Core 2.0 用户秘密和单元测试

Kil*_*ine 7 security unit-testing .net-core

我目前正在研究使用Microsoft 应用程序机密管理的 .net 核心解决方案(多个项目)。我可以在我的运行时项目(例如控制台应用程序)中拉回机密,但是当谈到在单独的项目(例如 Model.Test 项目)中利用用户机密进行集成测试时,正确的方法是什么?

我能想到几个选择:

  1. 给每个项目相同的UserSecretsId:这对于可能利用运行时项目使用的相同秘密的测试项目来说似乎是有意义的。

  2. 让每个项目都独一无二UserSecretsId:这将需要在开发机器上手动保持机密同步

似乎即使在 .net core 1.0 和 2.0 之间,用户机密也发生了一些变化,而且我对这个系统一般不太熟悉。谢谢!

Dal*_*e C 7

我自己仍在学习 .NET Core 2 中的配置。当我设置我的 API 项目时,我通过在项目和单元测试项目之间复制来共享用户机密标签。这意味着他们都有相同的用户机密 guid,就像您的选项 1。我的理由是,这将是为开发团队维护两个项目之间的机密的最简单方法。

Patrick Huber 的这篇文章展示了如何在对我来说很关键的配置构建器中引用用户秘密 - https://patrickhuber.github.io/2017/07/26/avoid-secrets-in-dot-net-core-tests .html

Rick Strahl 的这篇文章也很有用,它展示了如何在单元测试中配置测试助手以访问用户机密:https : //weblog.west-wind.com/posts/2018/Feb/18/Accessing-Configuration- in-NET-Core-Test-Projects

相关实现问题 -如何在 dotnet 核心测试项目中使用用户机密

与引用配置文件相关的问题 - AppSettings.json for Integration Test in ASP.NET Core


kku*_*lla 5

这提供了一个使用Dale C之前针对此问题提供的参考文献的具体示例。

它已经通过.NET6 进行了测试。该示例将从以下位置加载配置

  • 应用程序设置.json
  • 秘密.json
  • 环境变量

创建一个测试项目

在你的测试项目中

  1. 添加 nuget 包Microsoft.Extensions.Configuration.UserSecrets
  2. .csproj我们的测试项目的文件中,添加我们要访问的用户密钥的 guid。
<PropertyGroup>
    <UserSecretsId>0e8ea027-2feb-4098-ba69-4a6711e8eee2</UserSecretsId>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
  1. 创建一个测试类
using NUnit.Framework;
using MediatR;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;


namespace Example
{
    public class ConfigurationExampleTest
    {
        public IMediator? _mediator { get; set; }
        public IConfiguration config { get; set; }

        [SetUp]
        public void Setup()
        {            
            // The startupPath is simply one way of getting the path to
            // appettings.json. We could hard code tha path if necessary
            // or use any other method to obtain the path to appsettings.json
            var filepath = typeof(WebApp.Startup)
                .Assembly.Location;
            var StartupPath = Path.GetDirectoryName(filepath);

            config = new ConfigurationBuilder()
                .SetBasePath(StartupPath)
                .AddJsonFile("appsettings.json", optional: true)
                .AddUserSecrets<ConfigurationExampleTest>()
                .AddEnvironmentVariables()
                .Build();

           
            var host = Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(builder =>
                {
                    builder.UseStartup<WebApp.Startup>();
                })
                .ConfigureServices(services =>
                {
                    // Adds the config to the dependency injection
                    // This makes the config object accessible in
                    // your WebApp/project-under-test
                    services.AddSingleton(config);
                })
                .Build();
             
             // This will get any service we added to the dependency injection
             // in WebApp.Startup
            _mediator = host.Services.GetService<IMediator>();
        }

        [TearDown]
        public void Teardown()
        { }

        [Test]
        public void ExampleTest()
        {            
            // The configuration object will now be available
            // vai dependency ibjection

            // We can use the_mediator instance we got from the
            // dependency injection here
            
            // _mediator.Send(<your request>);
            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

参考: