dqwr32867 2017-02-07 09:31
浏览 106
已采纳

在PHP中获取Azure AD用户角色

I have a small application I am writing in PHP where I need to check if a given user has a given role assigned to them. I am using Azure App Service with app service authentication enabled to authenticate users against the azure active directory. As part of the application I need to get the users username, display name, and if they are a member of one or more of three security groups which they could be a part of to define what access levels they have within the application.

I need to do it this way as I can't have the roles managed within the application, instead it needs to be managed through active directory security groups. I am seeking a method that is native to azure app service (as in ideally not doing a separate LDAP lookup if possible). I know how to extract the authenticated username from the header data sent to the application (HTTP_X_MS_CLIENT_PRINCIPAL_NAME) however I don't know how I can get the full display name and how to check if a user has specific roles assigned to them. I have already output a copy of the entire php $_SERVER super global array to see if the data I am seeking is in there but I can't find it in there.

For the purposes of this question make the following assumptions...

user1@domain.com is assigned the roles role1, role2, role3
user2@domain.com is assigned the role role3 only

So if either user logs in I need to be able to show their full name based on their AD entry and need to be able to check if they are part of security groups role1 role2 and/or role3.

I would post an example of my code but I have no idea where to start with getting this data so the only code I have thus far is a test block to print all the $_SERVER values onto the page for testing purposes.

Thanks

  • 写回答

1条回答 默认 最新

  • duankan6894 2017-02-08 06:13
    关注

    Generally speaking, you can leverage Microsoft Graph REST APIs yo achieve your requirement.

    First of all, you can follow https://graph.microsoft.io/en-us/docs/authorization/app_only to implement the functionality to acquire access token in app only way, then you can make a HTTP request of List memberOf to get groups and directory roles that the user is a direct member of.

    The HTTP GET url should be like https://graph.microsoft.com/v1.0/users/{HTTP_X_MS_CLIENT_PRINCIPAL_NAME you get from header}/memberOf, and set the access token you get above in the authorization header.

    Furthermore, you can refer to https://github.com/Azure-Samples/active-directory-php-graphapi-directoryextensions-web/blob/master/AuthorizationHelperForGraph.php the sample for how acquire the access token in app only.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab