Microsoft身份验证库-获取组成员身份

时间:2019-10-02 17:30:29

标签: c# azure msal

我们现在在Xamarin / C#应用中将MSAL用于authenticate users,下一步是确定经过身份验证的用户是否是组织中允许访问该应用某些功能的某些组的成员。

我从querying the membership的StackOverflow中获取了代码,它抛出异常“ Authorization_RequestDenied:权限不足,无法完成操作”,但我已要求IT add the "groupMembershipClaims" property到mCP的Azure清单,并希望会解决问题。

以下是当前代码的重点:

var interactiveRequest = App.PCA.AcquireTokenInteractive(scopes);
var authResult = await interactiveRequest.ExecuteAsync();
var graphClient = new GraphServiceClient(
   new DelegateAuthenticationProvider(
      (requestMessage) =>
      {
         // Configure the HTTP bearer Authorization Header
         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
         return (Task.FromResult(0));
      }
      ));
   var groups = await graphClient.Me.MemberOf.Request().GetAsync();

我假设我可以使用它,但是现在我正在阅读here,了解Microsoft Graph API和Microsoft AD Graph API之间的区别,我不知道我找到的代码是否是在这里使用“正确”的代码,或者是否应该使用更简单/更好的代码。建议使用的确定组成员身份的代码吗?

编辑

IT部门将“ groupMembershipClaims”设置为“ SecurityGroup”,并且给出了相同的例外,因此显然查询成员资格与进行成员资格声明并不相同。但是在我的情况下,有一个组(角色?)说“允许此用户运行应用程序”,另一个组/角色说“该用户有权使用特殊功能”,所以我不希望身份验证请求如果用户不是第二个组/角色的成员,就会失败。

1 个答案:

答案 0 :(得分:1)

如果您想知道用户所属的组,可以分析id_token。 id_token有一个声明“ groups”,它将告诉您用户所属的组的ID。例如

  1. 配置应用清单

请将groupMembershipClaims的值更新为Allenter image description here

  1. 获取ID令牌
GET https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e        // Your registered Application ID
&response_type=id_token%20code
&redirect_uri=      // Your registered redirect URI, URL encoded
&response_mode=form_post                             // 'form_post' or 'fragment'
&scope=openid%20                                      // Include both 'openid' and scopes that your app needs  
offline_access%20                                         
https%3A%2F%2Fgraph.microsoft.com%2Fuser.read
&state=12345                                          // Any value, provided by your app
&nonce=678910                                         // Any value, provided by your app
  1. Alayse ID令牌vai jwt.io enter image description here
相关问题