首页 > 解决方案 > 从 Startup.cs 中的 API 拉取配置设置

问题描述

在我的Startup.cs课堂上,我有以下内容

    services.AddAuthentication(options =>
        {
            options.DefaultScheme = "cookie";
            options.DefaultChallengeScheme = "oidc";
            options.DefaultSignOutScheme = "oidc";
        })
        .AddCookie("cookie", options =>
        {
            options.Cookie.Name = "cookie name";
            options.Cookie.SameSite = SameSiteMode.Strict;
        })
        .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = "the authority URL";

            options.ClientId = "the client identifier";
            options.ClientSecret = "super secret";

            options.Scope.Clear();
            options.Scope.Add("openid");
            options.Scope.Add("profile");
            options.Scope.Add("api");
        });

与硬编码值相反,我想调用一个 API 来获取它们。API 将返回一个名为FooAuthenticationConfiguration.

标签: c#asp.net-coreblazor

解决方案


我不知道我是否正确地理解了您想要做什么,但请告诉我这是否适合您:

在 Startup.cs 中,创建如下方法:

        private async Task GetConfiguration()
        {
            HttpClient client = new HttpClient();
            var Response = await client.GetAsync("https://reqres.in/api/users/2");
        }

将 ConfigureServices 转换为 Async 方法并在 GetConfiguration() 开始时调用,如下所示:

    public async void ConfigureServices(IServiceCollection services)
    {
        await GetConfiguration();
        //....
    }

推荐阅读