azure 为什么在与Microsoft Graph成功交互验证后出现“拒绝访问错误”?

rsaldnfx  于 2023-02-16  发布在  其他
关注(0)|答案(2)|浏览(155)

我在本地应用程序和Azure门户中安装了Microsoft Graph。我可以使用自己的帐户成功登录,但当其他员工尝试登录时,我收到了成功的身份验证和访问令牌,但当调用InitializeGraphClientAsync()时,Microsoft.Graph.ServiceException抛出以下内容...

Exception thrown: 'Microsoft.Graph.ServiceException' in System.Private.CoreLib.dll
Failed to initialized graph client.
Accounts in the msal cache: 1.
See exception message for details: Code: ErrorAccessDenied
Message: Access is denied. Check credentials and try again.

登录:

public async Task<string> SignIn()
        {
            // First, attempt silent sign in
            // If the user's information is already in the app's cache,
            // they won't have to sign in again.
            var message = "";
            try
            {
                var accounts = await PCA.GetAccountsAsync();

                var silentAuthResult = await PCA.AcquireTokenSilent(Scopes, accounts.FirstOrDefault()).ExecuteAsync();

                Debug.WriteLine("User already signed in.");
                Debug.WriteLine($"Successful silent authentication for: {silentAuthResult.Account.Username}");
                Debug.WriteLine($"Access token: {silentAuthResult.AccessToken}");
                message = $"Successful silent authentication for: {silentAuthResult.Account.Username}";
            }
            catch (MsalUiRequiredException msalEx)
            {
                // This exception is thrown when an interactive sign-in is required.
                Debug.WriteLine("Silent token request failed, user needs to sign-in: " + msalEx.Message);
                message = "Silent token request failed, user needs to sign-in: " + msalEx.Message;
                // Prompt the user to sign-in
                var interactiveRequest = PCA.AcquireTokenInteractive(Scopes);

                if (AuthUIParent != null)
                {
                    interactiveRequest = interactiveRequest
                        .WithParentActivityOrWindow(AuthUIParent);
                }

                var interactiveAuthResult = await interactiveRequest.ExecuteAsync();
                Debug.WriteLine($"Successful interactive authentication for: {interactiveAuthResult.Account.Username}");
                Debug.WriteLine($"Access token: {interactiveAuthResult.AccessToken}");
                message = $"Successful interactive authentication for: {interactiveAuthResult.Account.Username}";
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Authentication failed. See exception messsage for more details: " + ex.Message);
                message = "Authentication failed. See exception messsage for more details: " + ex.Message;
            }
            await InitializeGraphClientAsync();

            return message;
        }

初始化

private async Task InitializeGraphClientAsync()
        {
            var currentAccounts = await PCA.GetAccountsAsync();
            try
            {
                if (currentAccounts.Count() > 0)
                {
                    // Initialize Graph client
                    GraphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
                        async (requestMessage) =>
                        {
                            var result = await PCA.AcquireTokenSilent(Scopes, currentAccounts.FirstOrDefault())
                                .ExecuteAsync();

                            requestMessage.Headers.Authorization =
                                new AuthenticationHeaderValue("Bearer", result.AccessToken);
                        }));

                    await GetUserInfo();

                    IsSignedIn = true;
                }
                else
                {
                    IsSignedIn = false;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to initialized graph client.");
                Debug.WriteLine($"Accounts in the msal cache: {currentAccounts.Count()}.");
                Debug.WriteLine($"See exception message for details: {ex.Message}");
                await SignOut();
            }
        }

这段代码是直接从微软的一个教程中提取出来的。
Azure :
API permissions
我将其配置为Accounts in any organizational directory (Any Azure AD directory - Multitenant)

lymnna71

lymnna711#

您可以尝试检查并遵循以下解决方法来解决此问题:

  • 验证您的应用接收的访问令牌类型是否与所寻求或授予的权限类型匹配。
  • 您可能在使用委托交互式代码流令牌而不是客户端凭据流令牌时请求并批准应用程序权限,或者您可能在使用客户端凭据流令牌而不是委托代码流令牌时请求并批准委托权限。
  • 请确保应用程序正在向Microsoft Graph发送有效的访问令牌作为请求的一部分。
  • 根据你的应用调用的Microsoft Graph API,检查你请求的权限是否准确。
    • 参考资料:**

Resolve Microsoft Graph authorization errors
Microsoft Graph permissions reference

6ju8rftf

6ju8rftf2#

验证您的应用接收的访问令牌类型是否与所寻求或授予的权限类型匹配。
看来我一定是使用具有更高权限的早期版本登录的。检查为我的帐户提供的令牌与另一名员工的令牌,结果显示权限较少。添加必要的权限消除了错误并解决了问题。

相关问题