【发布时间】:2022-02-17 23:51:50
【问题描述】:
我按照以下两个部分为函数应用创建托管标识并将其分配给 DocumentDB Account Contributor
Microsoft.Azure.Services.AppAuthentication
当我尝试运行以下部分中的代码时出现异常:
无法加载文件或程序集'System.Text.Encodings.Web, 版本=6.0.0.0,文化=中性,PublicKeyToken=cc7b13ffcd2ddd51'。 该系统找不到指定的文件。 在 System.Text.Json.Serialization.Metadata.JsonPropertyInfo.DeterminePropertyName() 在 System.Text.Json.Serialization.Metadata.JsonPropertyInfo.GetPolicies(Nullable
1 ignoreCondition, Nullable1 declaringTypeNumberHandling) 在 ... System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 Cosmos.Samples.AzureFunctions.AzureFunctionsCosmosClientMI.d__7.MoveNext() 在 C:.ME\MyLab.Code\AzureCode\CosmosDB\azure-cosmos-dotnet-v3-usage\AzureFunctions\AzureFunctionsCosmosClientMI.cs:line 85
Azure.Identity
由于 MS 不推荐AppAuthentication,所以我通过以下链接切换到使用Azure.Identity:
https://docs.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet
以及下面的代码
static string cosmosUrl = "https://xxx.documents.azure.com:443/";
private static CosmosClient client = new CosmosClient(cosmosUrl, new DefaultAzureCredential());
var container = client.GetContainer("FamilyDatabase", "FamilyContainer");
try
{
var result = await container.CreateItemAsync<Item>(data, new PartitionKey(data.LastName));
return new OkObjectResult(result.Resource.Id);
}
catch (CosmosException cosmosException)
{
log.LogError("Creating item failed with error {0}", cosmosException.ToString());
return new BadRequestObjectResult($"Failed to create item. Cosmos Status Code {cosmosException.StatusCode}, Sub Status Code {cosmosException.SubStatusCode}: {cosmosException.Message}.");
}
但是,我在本地和在 Azure 中运行时都遇到了以下异常。
创建项目失败。 Cosmos 状态码禁止,子状态码 5301:响应状态码不表示成功:Forbidden (403); 子状态:5301;活动编号:xxxx-bf03-4355-8642-5d316f9d3373; 原因:(请求被 Auth xxxx 阻止:请求被阻止,因为 主体 [xxx-2bff-44e9-97be-9ffeb3aae3ee] 没有 执行操作所需的 RBAC 权限 资源 [/] 上的 [Microsoft.DocumentDB/databaseAccounts/readMetadata]。 了解更多:https://aka.ms/cosmos-native-rbac。活动编号: xxx-bf03-4355-8642-5d316f9d3373, Microsoft.Azure.Documents.Common/2.14.0、Windows/10.0.14393 cosmos-netstandard-sdk/3.24.1);.
在本地,我通过链接登录 VS https://docs.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#authenticating-via-visual-studio
有解决 Azure.Identity 问题的想法吗?
参考:
【问题讨论】:
标签: azure azure-functions azure-cosmosdb azure-managed-identity