【发布时间】:2020-08-07 13:12:08
【问题描述】:
这里是菜鸟。我遵循了 IdentityServer 4 的快速入门。我正在清理代码。我正在使用网络核心 3.1。我的 Startup.ConfigureServices 变得拥挤,我想清理它并将配置选项、值放入一个类中——就像 IdentityServer 4 为 IdentityResources、ApiScopes、ApiResources 和客户端配置选项使用一个类一样。
我阅读了许多博客文章,并从https://andrewlock.net/avoiding-startup-service-injection-in-asp-net-core-3/ 中看到了如何为自定义服务添加配置到 IoC 容器,但我还没有找到一种方法来提取框架服务的选项/值,例如 Identity核心,或 services.AddAuthentication().AddOpenIdConnect() 例如。
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies", options =>
{
options.AccessDeniedPath = "/account/denied";
})
.AddOpenIdConnect("oidc", options =>
{
/*** HOW DO I PUT THE BELOW KEY/VALUES INTO A CONFIG CLASS ***/
options.Authority = "https://demo.identityserver.io";
options.ClientId = "server.hybrid";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.Scope.Clear();
options.Scope.Add("openid");
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
然后像这样使用那个配置类
var builder = services.AddIdentityServer()
.AddInMemoryIdentityResources(Config.IdentityResources) <-- From the Config class
.AddInMemoryApiScopes(Config.ApiScopes) <-- From the Config class
.AddInMemoryClients(Config.Clients); <-- From the Config class
所以如果我的问题不清楚,我该如何创建一个 Config 类并将该类传递给 .AddOpenIdConnect(MyConfigClass) 以清理 ConfigureServices 方法(IoC 容器?)
上面的代码是在 IoC 容器中配置服务的最佳/最干净的方式吗?是我在 Andrew Lock 的博客文章中寻找的答案,我只是不明白吗?我假设我可以像使用 IS4 .AddInMemoryIdentityResources 一样将一个类传递给 .AddOpenIdConnect 扩展方法
感谢您的帮助。
【问题讨论】:
标签: c# configuration .net-core-3.1