【发布时间】:2021-08-26 11:13:29
【问题描述】:
简短的问题陈述:
如何从 Cloud Foundry CredHub 中获取价值以在 ConfigureServices(IServiceCollection services) 内部使用?
现有状况:
假设我有一个名为SystemConfig 的类用于保存配置值。我在“开发”阶段使用 json 文件和 dotnet 机密的组合来存储这些值,并且可以通过
services.Configure<SystemConfig>(Configuration.GetSection("GLOBAL"));
在“SIT”阶段,我使用 Pivotal Cloud Foundry 来托管应用程序,并结合使用 ConfigServer - CredHub 来存储配置值。 CredHub 中的值可以通过
在Configure(IApplicationBuilder app) 中检索
var credhub = app.ApplicationServices.GetService<IOptions<CloudFoundryServicesOptions>>().Value.Services["credhub"].First(x => x.Name.Equals("credhub-instance"));
var sysConfig = app.ApplicationServices.GetService<IOptions<SystemConfig>>().Value;
sysConfig.SAMPLE_A = credhub.Credentials[nameof(sysConfig.SAMPLE_A)].Value;
当我需要从IdentityModel.AspNetCore 内部的ConfigureServices 配置访问令牌管理时出现问题:
services.AddAccessTokenManagement(x =>
{
x.Client.Clients.Add("key_sample", new IdentityModel.Client.ClientCredentialsTokenRequest()
{
Address = "sysConfig.SAMPLE_A",
ClientId = "taken from sysConfig as well",
ClientSecret = "taken from sysConfig as well"
});
});
方法AddAccessTokenManagement 只能在ConfigureService(IServiceCollection services) 内部使用,但同时我还没有来自CredHub 的值,因为它们是在Configure(IApplicationBuilder app) 内部检索到的。
也不建议使用services.BuildServiceProvider(),因为它会创建额外的单例服务副本,可能会导致意外错误。
所以问题归结为:
- 我们可以检索
ConfigureService(IServiceCollection services)中的 CredHub 值吗? - 如果不可能,那我们可以在
Configure(IApplicationBuilder app)中设置AddAccessTokenManagement吗?
【问题讨论】:
标签: .net-5 identitymodel steeltoe