因此,我正在try 将新的应用程序角色添加到我的Azure Active Directory应用程序.

请注意,在My Azure的API权限中,我在以下项下添加了添加读/写权限:用户、文件、角色管理和应用程序

我的代码是:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;

public async Task<string> TestAsync()
{
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    string clientId = "....";
    string tenantId = "....";
    string clientSecret = "...."; //Value and not Id
    string objectId = "....";

    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    
    var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

    var requestBody = new Application
    {
        AppRoles = new List<AppRole>
        {
            new AppRole
            {
                AllowedMemberTypes = new List<string>
                {
                    "User",
                    "Application",
                },
                Description = "TestRole1",
                DisplayName = "TestRole1",
                Id = Guid.NewGuid(),
                IsEnabled = true,
                Origin = "Application",
                Value = "test.write",
            }
        }
    };

    var result = await graphClient.Applications[objectId].PatchAsync(requestBody);

    return "role added!";
}

但是代码抛出了这个异常,并且NULL中的内部异常"抛出了‘Microsoft.Graph.Models.ODataErrors.ODataError’类型的异常".

我错过了什么?

推荐答案

最初,当我try 相同的代码时,我得到了如下所示的same error:

enter image description here

要找到100的详细信息,请使用try and catch来修改代码以捕获错误,如下所示:

    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var clientId = "ClientID";
    var tenantId = "TenantID";
    var clientSecret = "ClientSecret"; 
    var objectId = "ObjectID";

    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

    var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

    var requestBody = new Application
    {
        AppRoles = new List<AppRole>
        {
            new AppRole
            {
                AllowedMemberTypes = new List<string>
                {
                    "User",
                    "Application",
                },
                Description = "TestRole1",
                DisplayName = "TestRole1",
                Id = Guid.NewGuid(),
                IsEnabled = true,
                Origin = "Application",
                Value = "test.write",
            }
        }
    };
try
{
    var result = await graphClient.Applications[objectId].PatchAsync(requestBody);
}
catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error?.Code);
    Console.WriteLine(odataError.Error?.Message);
    throw;
}

Console.WriteLine("role added");

Now, I got the error details:

enter image description here

Note:要创建应用程序角色,您必须向Azure AD应用程序授予100应用程序API权限.

在我的例子中,我授予了API权限:

enter image description here

I am able to create app role successfully:

enter image description here

100

enter image description here

The error might occur if you are passing invalid client id, object id or no sufficient permission to perform the action.

Asp.net相关问答推荐

我可以将图像添加到 ASP.NET 按钮吗?

*不*使用 asp.net 会员提供程序是个坏主意吗?

你能从请求变量中确定时区吗?

/应用程序中的服务器错误. ASP.NET

jquery 1.9.0 和 modernizr 无法使用 ASP.NET Web 优化框架进行缩小

为什么 ASP.NET Identity 2.0 使用 GUID/字符串作为用户 ID?

ASP.NET MVC4 jquery/javascript 包的使用

如何在集线器类之外获取 SignalR 用户连接 ID?

FluentValidation 使用数据库的唯一名称验证

asp.net cookie、身份验证和会话超时

Ef core:执行 MaxAsync 时序列不包含任何元素

Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用

使用 LINQ 进行递归控制搜索

发布网站项目时临时路径太长

从 IFrame 重定向父页面

如何获得 System.Diagnostics.Process 的输出?

将 MemoryStream 写入响应对象

ASP.NET Excel 导出编码问题

如何使用 ConfigurationManager.AppSettings

DataTable 不包含 AsEnumerable 的定义