【问题标题】:Access token validation error MS Graph using UserCredentials使用 UserCredentials 访问令牌验证错误 MS Graph
【发布时间】:2017-06-25 22:11:07
【问题描述】:

我正在尝试使用 AAD 帐户访问 MS Graph,该帐户是全局管理员,并且拥有所有权限。我想在没有交互式登录的情况下执行此操作,即使用UserPasswordCredential。尝试访问 MS Graph 时出现错误:

我的流程:

获取令牌:

public async Task<string> GetUserAccessTokenAsync()
        {
            UserPasswordCredential userPasswordCredential = new UserPasswordCredential("user@tenant.onmicrosoft.com", "password");
            AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com");
            AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.windows.net", appId, userPasswordCredential);

            return token.AccessToken;
        }

使用令牌:

public static GraphServiceClient GetAuthenticatedClient()
        {
            GraphServiceClient graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        Adal adal = new Adal();
                        string accessToken = await adal.GetUserAccessTokenAsync();

                        // Append the access token to the request.
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                    }));
            return graphClient;
        }

尝试调用 MS Graph 读取事件:

 try
        {
            // Get events.
            items = await eventsService.GetMyEvents(graphClient);
        }
        catch (ServiceException se)
        {
            //this is where I get the error
        }

委派权限:

任何想法我哪里出错了?

【问题讨论】:

    标签: .net authentication azure-active-directory microsoft-graph-api adal


    【解决方案1】:

    经过进一步研究,我们确实需要管理员权限,因为我们使用的是 App-only 范围而不是委托 权限。 app-only 范围内的基本读取操作被指定为 Admin-only。希望这可以帮助遇到同样问题的人。 https://graph.microsoft.io/en-us/docs/authorization/permission_scopes

    【讨论】:

      【解决方案2】:

      经过更多研究,我了解到我们需要获得管理员权限。在文档中找到:“仅应用程序范围(也称为应用程序角色)授予应用程序范围提供的全部权限。仅应用程序范围通常由在没有登录用户的情况下作为服务运行的应用程序使用在场” https://graph.microsoft.io/en-us/docs/authorization/permission_scopes 我使用用户令牌获得了成功的结果,因为这是在委派权限下。 希望这对将来的某人有所帮助。

      【讨论】:

        【解决方案3】:

        至少你的资源 URI 是错误的。应该是:

        public async Task<string> GetUserAccessTokenAsync()
        {
           UserPasswordCredential userPasswordCredential = new UserPasswordCredential("user@tenant.onmicrosoft.com", "password");
           AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com");
           AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", appId, userPasswordCredential);
        
           return token.AccessToken;
        }
        

        https://graph.windows.net/ 用于 Azure AD Graph,而不是 MS Graph。

        对于 MS Graph API,您必须使用 https://graph.microsoft.com/

        【讨论】:

        • 主……有时我什至让我自己都感到惊讶。谢谢你,永远不会发现它。
        • 我在代码示例中打错了资源 URI,现在它已修复。
        猜你喜欢
        • 2017-05-19
        • 1970-01-01
        • 2019-07-16
        • 1970-01-01
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多