【问题标题】:Microsoft Graph API Scopes in a Xamarin applicationXamarin 应用程序中的 Microsoft Graph API 范围
【发布时间】:2017-06-21 15:19:34
【问题描述】:

我正在尝试在 Xamarin 应用程序中获取我所属的组。

现在,我正在用这种方法获得一个令牌:

    public async Task SignIn()
    {
        if (App.IdentityClientApp != null)
            this.authenticationResult = await App.IdentityClientApp.AcquireTokenAsync(new String[] { Constants.ApplicationID });
    }

我的应用 ID 来自 Apps.dev.microsoft.com 注册门户。

然后我用这个方法默默地获取另一个令牌:

    private async Task GetTokenForGroups()
    {
        this.authenticationResult = await App.IdentityClientApp.AcquireTokenAsync(new String[] { "User.Read", "Directory.AccessAsUser.All" }, null);
    }

如果我这样做,系统会提示我使用管理员帐户登录。 如果这样做,令牌获取将成功,但不是“记住”其他非管理员用户的授权,而是每次都提示我授予管理员权限。

如果我只使用“User.Read”范围进行此调用:

    public void GetAuthenticatedClient()
    {
        try
        {
            this.graphClient = new GraphServiceClient(
                "https://graph.microsoft.com/v1.0",
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        await GetTokenForGroups();
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", this.authenticationResult.Token);
                    }));
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Could not create a graph client: " + ex.Message + ex.InnerException);
        }
    }

    public async void GetGroups()
    {
        this.GetAuthenticatedClient();
        var groups = await graphClient.Me.MemberOf.Request().GetAsync();  
    }

我得到了一些组,但它们似乎是 Outlook 组,而不是安全组。

我在 apps.dev.microsoft.com 中的 Graph 权限完全为空。

我怎样才能读取目录数据?我做错了什么?

谢谢。

【问题讨论】:

    标签: c# xamarin.forms microsoft-graph-api msal


    【解决方案1】:

    您需要使用管理员同意工作流程。仅以管理员身份登录并不表示同意非管理员用户。有一个特定的工作流程可以实现这一点。

    看看我写的一篇名为v2 Endpoint and Admin Consent的文章。它应该为您提供一个连接同意过程的起点。

    【讨论】:

    • 非常感谢您的明确答复。重定向uri重要吗?还是只是为了在 conset 之后提供更友好的导航?
    • 必填,但可以是任何页面。它确实会返回数据,告诉您哪些租户获得了同意,这对于跟踪哪些用户已获得同意很有用。
    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多