从ACS返回AD组成员资格

时间:2014-04-03 16:00:30

标签: c# authentication azure adfs2.0 acs

这是我在这里发表的第一篇文章,所以请温柔地对待我! :)

我正在尝试使用Azure ACS(即Office 365)作为身份验证系统开发应用程序。事情的身份验证方面似乎运行良好,我被重定向到Office 365登录页面,并可以成功登录。我的代码然后使用Graph API获取有关该用户的其他信息。

我正在努力获取登录人员所属的AD群组列表。当我请求组列表时,只返回Office 365安全角色。

这就是我所做的(我希望我已经包含了所有相关内容)

从Azure内部;

  • 进入活动目录
  • 选择了我的域名(来自本地广告的DirSyncs)
  • 创建了一个新应用程序
  • 允许读取目录数据并启用登录和读取用户的个人资料
  • 创建了一个新密钥

从我的测试应用中;

  • 创建了一个新的Web应用程序(标准Web表单)
  • 使用“身份和访问”设置指定“用户提供商家标识”
  • 将clientID和密码添加到app.settings文件
  • 添加了Windows Azure Active Directory图形帮助程序项目

这是我的相关代码

        IPrincipal myPrincipal = this.User;

        //get the tenantName
        string tenantName = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;

        // retrieve the clientId and password values from the Web.config file
        string clientId = Properties.Settings.Default.ClientID;
        string password = Properties.Settings.Default.Password;

        // get a token using the helper
        AADJWTToken token = DirectoryDataServiceAuthorizationHelper.GetAuthorizationToken(tenantName, clientId, password);

        // initialize a graphService instance using the token acquired from previous step
        DirectoryDataService graphService = new DirectoryDataService(tenantName, token);

        User myUser = graphService.users.Where(it => (it.userPrincipalName == myPrincipal.Identity.Name)).SingleOrDefault();
        graphService.LoadProperty(myUser, "memberOf");
        List<Role> currentRoles = myUser.memberOf.OfType<Role>().ToList();

当我运行我的代码时,currentRoles只包含'公司管理员',而不包含我所属的其他组。

我读过很多关于如何通过命名空间添加规则的文章,但这似乎与使用Windows ACS有关。

我可能错过了一个非常基本的步骤,并会永远感激在正确的方向上推动:)

谢谢, 达伦

1 个答案:

答案 0 :(得分:0)

假设您的代码很好(仅使用代码段很难分辨),您的主要问题是您使用Role代替Group。角色和组是图中的不同概念。图表返回的角色不适用于基于角色的访问控制(RBAC),而组可以(并且应该)用于此目的。 See my other answer here for more information

此外,在不知道应用程序需求的情况下,如果需要对多个身份提供程序进行身份验证,则应该只使用ACS。看起来您只是使用Azure AD作为您的IdP,因此您可以直接对服务进行身份验证,而不是使用ACS作为中间人。

Azure AD中的

This topic about authorization and RBACaccompanying code sample应该可以帮助您了解有关如何在Azure AD和图表中使用角色和组的更多信息。

相关问题