azure-web-app-service - Azure Key Vault 未从应用设置中检索 ClientId 或 ClientSecret
问题描述
我正在尝试从我的 ASP.NET MVC Web 应用程序中使用 Azure Key Vault,并且我正在按照这些说明进行操作。
我的 Web.config 看起来像这样(与说明中的相同):
<!-- ClientId and ClientSecret refer to the web application registration with Azure Active Directory -->
<add key="ClientId" value="clientid" />
<add key="ClientSecret" value="clientsecret" />
<!-- SecretUri is the URI for the secret in Azure Key Vault -->
<add key="SecretUri" value="secreturi" />
我获取访问令牌的方法如下所示(与说明相同):
//add these using statements
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;
using System.Web.Configuration;
//this is an optional property to hold the secret after it is retrieved
public static string EncryptSecret { get; set; }
//the method that will be provided to the KeyVaultClient
public static async Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
ClientCredential clientCred = new ClientCredential(WebConfigurationManager.AppSettings["ClientId"],
WebConfigurationManager.AppSettings["ClientSecret"]);
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token");
return result.AccessToken;
}
我已将 ClientId、ClientSecret 和 SecretUri 放入我的 Web 应用程序的应用程序设置中,就像说明中的屏幕截图一样。既然我这样做了,我可以期望(根据说明):
如果您有 Azure Web 应用程序,您现在可以在 Azure 门户中添加 AppSettings 的实际值。通过这样做,实际值将不在 web.config 中,而是通过您拥有单独访问控制功能的门户进行保护。这些值将替换您在 web.config 中输入的值。确保名称相同。
但是,当我运行上述方法时,WebConfigurationManager.AppSettings["ClientId"]
解析到的值clientid
是虚拟值,ClientSecret 也是如此。我的理解是,该方法应该与 Azure 门户中的 Web 应用程序联系并替换这些值。我错过了什么?为什么没有被替换的值?
编辑:另外,我使用 Azure Active Directory B2C 而不是 Azure Active Directory 可能很重要。
解决方案
当您从本地环境运行或调试应用程序时,应用程序会从 web.config 中选择值,因此您会在网页上看到虚拟值。当您将应用程序部署到 Azure 时,您的应用程序将从 Azure 应用程序设置中选择值。此外,您需要在 web.config 以及 Azure 应用程序设置中保持密钥名称相同。希望这可以帮助。
推荐阅读
- java - android.speech.tts.TextToSpeech.setLanguage(java.util.Locale)' 在空对象引用上
- python - 如何获取数组中搜索元素的索引并更新该行Python中的最后一个元素
- itext - 使用 iText 在现有 pdf 的每一页中添加 TextField
- python - 提高 U-Net 分割模型的训练精度
- php - 将整个表数据发送到php中的另一个表
- mongodb - 如何从运行在同一主机上的 Kubernetes 集群内部连接到 MongoDB 数据库?
- c# - 在 C# 中,如何使用 `TryAsync` 的实例?
- html - 为什么 HTML 和 Body 标签在我的编辑器中标记为红色?
- c - 循环直到用户输入在 C 中正确
- javascript - 如何将自定义键添加到猫鼬模式项?