【问题标题】:List All Joined Teams列出所有加入的团队
【发布时间】:2017-08-24 18:48:29
【问题描述】:

我有一个网站,让用户使用 Azure AD 登录,然后我试图获取用户所属的所有 Microsoft 团队的列表。我正在使用 GraphServiceClient,它似乎没有测试版的东西,但我想我已经解决了这个问题,请参阅下面的示例,如果有更好的方法,请告诉我。

首先,当我创建 GraphServiceClient 时,我会像这样向构造函数提供 beta 端点。

public GraphServiceClient GetAuthenticatedClient(string userId) {
 _graphClient = new GraphServiceClient("https://graph.microsoft.com/beta", new DelegateAuthenticationProvider(
  async(requestMessage) => {
   // Passing tenant ID to the sample auth provider to use as a cache key
   AccessToken = await _authProvider.GetUserAccessTokenAsync(userId);

   // Append the access token to the request
   requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
  }));

 return _graphClient;
}

您还会注意到我正在存储 AccessToken,因为我稍后需要它,这就是为什么我质疑是否有更好的方法来处理这个问题。

然后在我尝试获取已加入团队列表的方法中,我正在执行以下操作。

public static async Task < IEnumerable < string >> GetGroups(GraphServiceClient graphClient) {
 var request = graphClient.Me.Request().GetHttpRequestMessage();
 request.RequestUri = new Uri(request.RequestUri.AbsoluteUri + "/joinedTeams");

 //request.RequestUri = new Uri("https://graph.microsoft.com/beta/groups");
 request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", GraphSDKHelper.AccessToken);

 request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

 try {
  var response = await graphClient.HttpProvider.SendAsync(request);
  var stream = await response.Content.ReadAsStreamAsync();
 } catch (Exception e) {}

 //var gs = await graphClient.Me.MemberOf.Request().GetHttpRequestMessage();//.GetAsync();
 var results = new List < string > ();
 var groups = await graphClient.Groups.Request().GetAsync();
 do {
  foreach(var group in groups) {
   results.Add(group.DisplayName);
  }
  groups = await groups.NextPageRequest.GetAsync();
 } while (groups.NextPageRequest != null);

 return results;
}

您可以看到这有 2 个部分。我正在尝试使用加入的团队通话的第一部分,但这给了我以下错误。然后我只是列出了所有组并且那个组工作正常。

Message: Authorization has been denied for this request.

Inner error

在 portal.azure.com 中,我进行了应用注册设置并授予了 user.read.all 应用程序权限

还有 user.read.all 在委派权限下(不确定我需要给它哪个)。

那么为什么这不起作用,如果还有其他事情我做的不对,请告诉我。

经过更多测试后的补充说明。问题真的是我。每当我和我一起打电话时,即我/驱动器等,它都会出错。我可以调用 users[XXXXX]/drives 并且工作正常,但是我下面的任何东西都失败了。

已编辑

他是创建GraphServiceClient 的代码,根据下面Marc 的回答,这可能是我无法访问me 问题的原因:

public GraphServiceClient GetAuthenticatedClient(string userId)
{
    _graphClient = new GraphServiceClient("https://graph.microsoft.com/beta", new DelegateAuthenticationProvider(
        async(requestMessage) =>
        {
            // Passing tenant ID to the sample auth provider to use as a cache key
            AccessToken = await _authProvider.GetUserAccessTokenAsync(userId);

            // Append the access token to the request
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
        }));

    return _graphClient;
}

public async Task<string> GetUserAccessTokenAsync(string userId)
{
    TokenCache userTokenCache = new SessionTokenCache(userId, _memoryCache).GetCacheInstance();

    try
    {
        AuthenticationContext authContext = new AuthenticationContext(_aadInstance, userTokenCache);
        ClientCredential credential = new ClientCredential(_appId, _appSecret);
        AuthenticationResult result = await authContext.AcquireTokenAsync(_graphResourceId, credential);

        return result.AccessToken;
    }
    catch (Exception ex)
    {
        return null;
    }
}

【问题讨论】:

    标签: azure-active-directory microsoft-graph-api


    【解决方案1】:

    如果您使用的是应用程序权限(又称“客户端凭据流”),则无法使用 /me/ 调用任何方法。 /me/ 路径要求上下文中有用户。这只发生在用户已通过身份验证并且您正在使用委派权限(又名“授权授予”或“隐式授予”流程)时。

    特别是joinedTeams 方法还有一个known issue,目前将其限制为me 用户,有效地使应用场景变得不可能。

    我建议转向具有委托范围的授权授予流程并要求用户进行身份验证。这将启用me 用户上下文并允许您检索joinedTeams

    至于存储令牌,这不是一个好的解决方案。代币的寿命相对较短。相反,您应该请求“离线访问”权限。这将启用refresh_token,您可以根据需要存储和使用它来检索新的access_token

    【讨论】:

    • 用户正在通过 Azure AD 登录应用程序。那么我需要做些什么来旋转委派的权限。我都已经核对了申请注册。
    • 我编辑了我的问题以显示我在创建 GraphServiceClient 时使用的所有代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多