簡體   English   中英

.net5 secrets.json 未在本地使用

[英].net5 secrets.json not being used locally

我有一個 .net5 web 應用程序,我試圖在其中添加一個 secrets.json 文件,所以我右鍵單擊並創建了該文件 - 在我的項目文件中我可以看到:

<PropertyGroup>
  <TargetFramework>net5.0</TargetFramework>
  <UserSecretsId>1cb5a573-309c-4b03-b7e9-3c09794038bb</UserSecretsId>
</PropertyGroup>

AppData\Roaming\Microsoft\UserSecrets\1cb5a573-309c-4b03-b7e9-3c09794038bb 中存在 secrets.json 文件:

{
  "GraphApi": {
    "ClientSecret": "secret-key"
  },
}

在我的 appsettings.json 中:

"GraphApi": {
  "TenantId": "tenant-id",
  "Scopes": ".default",
  "ClientId": "client-id",
  "ClientSecret": "From user secrets"
},

在我的 program.cs 中:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .ConfigureAppConfiguration((hostContext, builder) =>
            {
                if (hostContext.HostingEnvironment.IsDevelopment())
                {
                    // Use user secrets for local development
                    builder.AddUserSecrets<Program>();
                }
            });

但是,它沒有從 secrets.json 中獲取我的ClientSecret - 如果我將值放入 appsettings.json,它工作得很好 - 我還需要做些什么才能讓它工作嗎?

原來有一個 web.config 添加到項目中,它 wsa 指向項目使用覆蓋秘密的 appsettings.development 版本:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="bin\Debug\net5.0\Project.WebApp.exe" arguments="" stdoutLogEnabled="false" hostingModel="InProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="443" />
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

刪除此文件並重新配置項目以使用 iis express 而不是 iis 解決了這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM