如何使用 local.settings.json 文件的内容设置我的 Azure 函数?

How do I set up my Azure Function with the content of the local.settings.json file?

我的问题很具体,但我希望这里有人能够帮助我...

好吧长话短说

我正在用 C#(使用 .NET Core 3.1)开发一个 Azure 函数,它使用从 SharePoint 列表中获取的数据输出 PowerPoint 幻灯片,以及一个也存储在所述 SharePoint 上的幻灯片模板。为此,我使用 ConfigurationBuilderlocal.settings.json 文件中加载配置。
这个文件看起来像这样:

"IsEncrypted": false,
"Values": {
  "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  "FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"TokenEndpoint": "https://login.microsoftonline.com/common/oauth2/token",
"User": "!! put this line filled with the email of the account in your secrets.json !!",
"Password": "!! put this line filled with the password of the account in your secrets.json !!",
"AzureAppId": "some guid",
"SharePointSite": "https://something.sharepoint.com/sites/something_else",
"TemplatesConfig": {
  "Path": "Shared%20files/",
  "FirstTemplateName": "FirstTemplate.pptx",
  "SecondeTemplateName": "SecondTemplate.pptx"
}

为了连接到 SharePoint,我们使用存储在此文件中的电子邮件和密码,但为了避免凭据泄漏的任何问题,我使用 user secrets 我正在“添加”配置生成器用实际凭据替换占位符凭据。
然后,我将进入 TemplatesConfig 项并阅读 PathFirstTemplateNameSecondTemplateName 属性以了解在哪里搜索模板文件,以及第一个文件的名称和第二个模板。
我可以 运行 在本地运行该功能,一切正常。好。

问题从哪里开始

现在,我想把函数发布到云端使用。函数应用程序已创建并基本设置完毕。我可以发布它,但不能 运行 它...问题来自配置生成器无法读取 TemplatesConfig 因为它未定义。

阅读文档并在一些论坛上搜索答案,我发现配置文件 local.settings.json 显然只在执行函数 locally 时使用,你可以'不要像我尝试的那样在发布时使用它。相反,需要在 Azure 的应用程序设置中输入此文件中的键值: 问题是在应用程序设置中输入的密钥是您可以在配置 JSON 的 Values 项中输入的密钥,而那不是我的配置数据所在的位置......此外,它看起来像你只能存储键值关联,但我正在存储包含这些的整个项目...

根据 documentationlocal.settings.json 中可能有多个部分,其中两个似乎链接到我在上面的屏幕截图中向您展示的页面的两个部分(应用程序设置和连接字符串),所以我认为这是搜索答案的正确位置,但感觉更像是我将不得不 从头开始​​重新制作我的凭据和模板数据存储系统...
这是我第一次制作 Azure 函数。事实上,其他人开始了这项工作,然后在构建了整个“基础”时将其传递给我(其中包括程序的这一部分,这让我很烦恼,因为我很难理解它)。

那么我的问题是什么?

您是否知道我应该如何正确存储有关模板的信息以及连接到 SharePoint 的凭据?当前的解决方案是正确的,还是我完全错过了另一种我应该用来存储它的机制?我是否应该完全重做我的身份验证方法并使用 Connection Strings 连接到 SharePoint 而不是将凭据存储为密钥?

非常感谢您花时间阅读我,并提前感谢您的回复。

我怀疑您的凭据有问题。

如果我没记错的话,当你发布的时候,用户机密是没有的。仅用于开发。

如标​​题中所述,您不应在生产中使用应用机密:https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-5.0&tabs=windows

您应该改用环境变量:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0

尝试一下,并用有关问题的更多详细信息补充您的问题,以便我们为您提供更多帮助。

如果我没理解错的话,你不能像下面这样简单地设置你的设置吗?

"IsEncrypted": false,
"Values": {
  "AzureWebJobsStorage": "UseDevelopmentStorage=true",
  "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  "TemplatesConfig:Path": "Shared%20files/",
  "TemplatesConfig:FirstTemplateName": "FirstTemplate.pptx",
  "TemplatesConfig:SecondeTemplateName": "SecondTemplate.pptx"
},
"TokenEndpoint": "https://login.microsoftonline.com/common/oauth2/token",
"User": "!! put this line filled with the email of the account in your secrets.json !!",
"Password": "!! put this line filled with the password of the account in your secrets.json !!",
"AzureAppId": "some guid",
"SharePointSite": "https://something.sharepoint.com/sites/something_else"    

以及您的应用程序设置

姓名:TemplatesConfig:Path
值:Shared%20files/ 姓名:TemplatesConfig:FirstTemplateName
值:第一个模板名称

...

好的,我带着在你们和我同事的帮助下找到的解决方案回来了。

我所做的是将 local.settings.json 文件中的所有内容都放入 Values 对象中,就像这样:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "SharePointSite": "https://something.sharepoint.com/sites/something_else",
    "User": "!! put this line filled with the email of the account in your secrets.json !!",
    "Password": "!! put this line filled with the password of the account in your secrets.json !!",
    "TemplatesConfig:Path": "Shared%20files/",
    "TemplatesConfig:FirstTemplateName": "FirstTemplate.pptx",
    "TemplatesConfig:SecondTemplateName": "SecondTemplate.pptx"
  },
  "TokenEndpoint": "https://login.microsoftonline.com/common/oauth2/token",
  "AzureAppId": "some guid"
}

我了解到 Azure 仅使用环境变量,并且 Visual Studio 将它们放入此文件的 Values 对象中以在开发时“模拟”环境变量。

在我的配置加载代码中,我只需添加一行 Microsoft.Extensions.Configuration.ConfigurationBuilder.AddEnvironmentVariables() 即可在函数 运行 在线(发布后)时从 Azure 获取它们。现在,在本地 运行 很好,最重要的是,在网上也很好。

至于凭据,在我的 secret.json 文件中,我只需要将电子邮件和密码的两行也放在 Values JSON 对象中,就像在 local.settings.json 中,瞧,它在开发和在线时也能完成这项工作。

有关更多信息,我检查了这里的人回复的链接,还有这个文档:

谢谢大家的帮助!