如何从方法体中提取配置选项并在 Dot Net Core 3.1 中使用配置 class
How to pull configuration options out of method body and use configuration class in Dot Net Core 3.1
菜鸟在这里。我遵循了 IdentityServer 4 的 QuickStarts。我正在清理代码。我正在使用 Net Core 3.1。我的 Startup.ConfigureServices 变得拥挤,我想清理它并将配置选项、值放入 class - 就像 IdentityServer 4 使用 class 作为IdentityResources、ApiScopes、ApiResources 和客户端配置选项。
我读了很多博客 posts 并且我从 https://andrewlock.net/avoiding-startup-service-injection-in-asp-net-core-3/ 中了解到如何将配置添加到 IoC 容器以用于自定义服务,但我还没有找到一种方法来提取options/values 用于框架服务,例如 Identity Core,或 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"
};
});
并像这样使用该配置 class
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
因此,如果我的问题不清楚,我如何制作配置 class 并将 class 传递到 .AddOpenIdConnect(MyConfigClass)
以清理 ConfigureServices 方法(IoC 容器? )
以上代码是best/cleanest在IoC容器中配置服务的方式吗?我在 Andrew Lock 的博客 post 中寻找的答案是我只是不明白吗?我假设我可以像使用 IS4 .AddInMemoryIdentityResources
一样将 class 传递到 .AddOpenIdConnect 扩展方法中
感谢您的帮助。
所有 Microsoft 服务都遵循相同的 Options pattern。首先,您可以将这些 lambda 方法移动到静态方法,也许在不同的 类 上。这似乎是您的 Config
示例所做的。您只需要确保您的静态方法具有相同的函数签名。
例如,.AddAuthentication
将 Action<AuthenticationOptions>
委托作为参数。因此,可以传入任何 static void foo(AuthenticationOptions o)
方法。
或者您可以编写实现 IConfigureOptions<T>
的服务并注册这些服务。如果您需要访问其他服务(例如数据库)以配置这些选项类型,这将特别有用。
菜鸟在这里。我遵循了 IdentityServer 4 的 QuickStarts。我正在清理代码。我正在使用 Net Core 3.1。我的 Startup.ConfigureServices 变得拥挤,我想清理它并将配置选项、值放入 class - 就像 IdentityServer 4 使用 class 作为IdentityResources、ApiScopes、ApiResources 和客户端配置选项。
我读了很多博客 posts 并且我从 https://andrewlock.net/avoiding-startup-service-injection-in-asp-net-core-3/ 中了解到如何将配置添加到 IoC 容器以用于自定义服务,但我还没有找到一种方法来提取options/values 用于框架服务,例如 Identity Core,或 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"
};
});
并像这样使用该配置 class
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
因此,如果我的问题不清楚,我如何制作配置 class 并将 class 传递到 .AddOpenIdConnect(MyConfigClass)
以清理 ConfigureServices 方法(IoC 容器? )
以上代码是best/cleanest在IoC容器中配置服务的方式吗?我在 Andrew Lock 的博客 post 中寻找的答案是我只是不明白吗?我假设我可以像使用 IS4 .AddInMemoryIdentityResources
一样将 class 传递到 .AddOpenIdConnect 扩展方法中感谢您的帮助。
所有 Microsoft 服务都遵循相同的 Options pattern。首先,您可以将这些 lambda 方法移动到静态方法,也许在不同的 类 上。这似乎是您的 Config
示例所做的。您只需要确保您的静态方法具有相同的函数签名。
例如,.AddAuthentication
将 Action<AuthenticationOptions>
委托作为参数。因此,可以传入任何 static void foo(AuthenticationOptions o)
方法。
或者您可以编写实现 IConfigureOptions<T>
的服务并注册这些服务。如果您需要访问其他服务(例如数据库)以配置这些选项类型,这将特别有用。