MsalServiceException:AADSTS500011:找不到名为https://graph.microsoft.com/v1.0的资源主体

时间:2020-09-08 06:48:29

标签: c# asp.net azure-active-directory microsoft-graph-api

范围值=“ https://graph.microsoft.com/.default”或“ https://graph.microsoft.com/beta”

在asp.net c#中给出err以下的内容。

MsalServiceException:AADSTS500011:资源主体名为 在名为的租户中找不到https://graph.microsoft.com/v1.0 'xxxxxxxx'。如果尚未安装该应用程序,则会发生这种情况 由租户的管理员或任何用户同意 承租人。您可能发送了错误的身份验证请求 租户。

代码:

string clientId = AppClientID;
        string clientSecret = Secret;
        string redirectUri =`enter code here` System.Configuration.ConfigurationManager.AppSettings["redirectUri"]; 
        string authority = "https://login.microsoftonline.com/" + tenantID;              
        string[] scopes = new string[] { "https://graph.microsoft.com/.default" };        
        //string[] scopes = new string[] { "https://graph.microsoft.com/beta/.default" };        
        IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
            .Create(clientId)
            .WithRedirectUri(redirectUri)
            .WithClientSecret(clientSecret)
            .WithAuthority(authority)
            .Build();
        AuthorizationCodeProvider auth = new AuthorizationCodeProvider(app, scopes);
        GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
        {           
            var authResult = app.AcquireTokenForClient(scopes).WithAuthority(authority, true).ExecuteAsync().Result.AccessToken.ToString();
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult);
        }));      
        var onlineMeeting = new OnlineMeeting
        {
            StartDateTime = DateTimeOffset.Parse("2021-07-12T21:30:34.2444915+00:00"),
            EndDateTime = DateTimeOffset.Parse("2021-07-12T22:00:34.2464912+00:00"),
            Subject = "My First MS Teams Meeting"
        };
        await graphServiceClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting);

1 个答案:

答案 0 :(得分:0)

  1. 如果我将“ 范围”设置为https://graph.microsoft.com/v1.0/.default,则可以重现您的问题,因此请确保将“范围”设置为https://graph .microsoft.com/.default

enter image description here

  1. 您不能使用身份验证代码流中的[AcquireTokenForClient][2]函数来获取令牌。它通常应用于客户端凭证流。此流程不需要用户登录,因此,即使您使用此功能获取令牌,也不正确。您可以解析要查看令牌,它不具有您在门户中添加的权限。对于身份验证代码流,您应使用AcquireTokenByAuthorizationCode来获取令牌,如Pamela所述。

使用AcquireTokenByAuthorizationCode获取令牌并进行解析:

enter image description here enter image description here

3。代码:

            string clientId = "{clientId}";
            string clientSecret = "{clientSecret}";
            string redirectUri = "{redirectUri}";
            string authority = "https://login.microsoftonline.com/{tenant id}";
            string authorizationCode = "code";

            string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

            IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithRedirectUri(redirectUri)
                .WithClientSecret(clientSecret)
                .WithAuthority(authority)
                .Build();

            AuthorizationCodeProvider auth = new AuthorizationCodeProvider(app, scopes);

            GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {

                // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
                var authResult = await app.AcquireTokenByAuthorizationCode(scopes, authorizationCode).ExecuteAsync();

                // Add the access token in the Authorization header of the API request.
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);

            })
            );

            var onlineMeeting = new OnlineMeeting
            {
                StartDateTime = DateTimeOffset.Parse("2021-07-12T21:30:34.2444915+00:00"),
                EndDateTime = DateTimeOffset.Parse("2021-07-12T22:00:34.2464912+00:00"),
                Subject = "My First MS Teams Meeting"
            };

            await graphServiceClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting);