我已经编写了一个函数应用程序,在代码的开头我有:
var credential = new ManagedIdentityCredential();
var client = new SecretClient(vaultUri, credential);
在 Azure 中,我转到函数应用程序的“身份”部分,并将系统分配的状态设置为“开”。我还转到密钥保管库,并使用“访问控制 (IAM)”,将托管身份添加为密钥保管库机密用户。
如果我在我的功能应用程序中进入访问控制,我已将托管角色分配为贡献者,并将托管应用程序贡献者角色。
我不知道还需要设置什么才能让托管用户从我的应用程序内访问密钥保管库。
我还将全部功能发布为独立的。
函数应用程序调用部分的错误开始
结果:失败异常:Azure.RequestFailedException:调用者无权对资源执行操作。如果最近更改了角色分配、拒绝分配或角色定义,请观察传播时间。调用者:appid=61c8be63-9b5d-4c29-80ec-c839a0f0a61c;oid=849fa139-74c5-4adb-9b94-74bc444b68c0;iss=https://sts.windows.net/2f77693b-b7cd-4918-8d8a-d4f28910516f/操作:“Microsoft.KeyVault/vaults/secrets/getSecret/action”资源:“/subscriptions/fb54f07b-964f-4366-88e2-
我从下面的回答中了解到 Ikhtesam Shots:
在我的 Program.cs 文件中我有:
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((context,services) =>
{
var vaultUri = new Uri("https://{Key Vault Name Here}.vault.azure.net/");
var credential = new ManagedIdentityCredential();
var client = new SecretClient(vaultUri, credential);
services.AddSingleton(client);
services.AddTransient<ISecretService, SecretService>();
然后在我的函数应用程序中我有:
public class GetMongoData
{
private readonly ILogger _logger;
private readonly IMySqlSession _session;
private readonly IMySqlClaimants _claimants;
private readonly IMySqlAddresses _addresses;
private readonly IMySqlClaims _claims;
private readonly IMySqlDefendants _defendants;
private readonly IMySqlDuplicateClaims _duplicateClaims;
private readonly IMySqlEvidence _evidence;
private readonly IMySqlStatuses _statuses;
private readonly ISecretService _secretService;
private readonly string _sshPath;
private readonly string _mySqlServerIp;
public GetMongoData(ILoggerFactory loggerFactory, IConfiguration
configuration, IMySqlSession mySqlSession, IMySqlClaimants mySqlClaimants,
IMySqlAddresses mySqlAddresses, IMySqlClaims mySqlClaims, IMySqlDefendants defendants, IMySqlDuplicateClaims mySqlDuplicateClaims,
IMySqlEvidence mySqlEvidence, IMySqlStatuses mySqlStatuses, ISecretService secretService)
{
_logger = loggerFactory.CreateLogger<GetMongoData>();
_session = mySqlSession;
_secretService = secretService;
_claimants = mySqlClaimants;
_addresses = mySqlAddresses;
_claims = mySqlClaims;
_defendants = defendants;
_duplicateClaims = mySqlDuplicateClaims;
_evidence = mySqlEvidence;
_statuses = mySqlStatuses;
_mySqlServerIp = _secretService.GetSecret("MySqlServerIp");
_defendants = defendants;
}
[Function("RetrieveMongoData")]
public void Run([TimerTrigger("0 0 * * * *")] TimerInfo myTimer)
{ //function set to run every hour.
_logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
try
{
var server = _mySqlServerIp;
var sshUserName = "forge";
var sshPassword = "";