未加星标

AzureFunctions, PowerShell, MS Graph and AppOnly permission

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

In a previousblog post, I wrote about connecting to Microsoft Graph with Resource Owner grant.

That particular authentication scheme is for delegate permissions. So the function, utilizing an account's username/password, is performing actions as that user.

I'm revisiting that post today and instead talk about App-Only Permissions, and the steps are very similar, and to some may be easier.

Register App-Only

Head into the New Azure Portal.


AzureFunctions, PowerShell, MS Graph and AppOnly permission
AzureFunctions, PowerShell, MS Graph and AppOnly permission

Add "Read and write all groups" permission under "Application Permissions"

You should see this:


AzureFunctions, PowerShell, MS Graph and AppOnly permission

"1 Application Permission"

Hit the Grant Permission button here.


AzureFunctions, PowerShell, MS Graph and AppOnly permission

And set up a Client Secret


AzureFunctions, PowerShell, MS Graph and AppOnly permission

Nearly there, one more step we need to make the registered app allow implicit flow.

Hit the Manifest button, change oauth2AllowEmplicitFlow property to true, hit save.


AzureFunctions, PowerShell, MS Graph and AppOnly permission

Copy the Application ID - this is the Client ID.

On the first page of Azure AD properties, grab the Directory ID


AzureFunctions, PowerShell, MS Graph and AppOnly permission

You now have:

Directory ID (Tenant ID) Application ID (Client ID) Key (Client Secret) And consent for your app has been granted. PowerShell Code $tenant_id = "26e65220-5561-46ef-9783-ce5f20489241"; $client_id = "44da3f20-fc0b-4f90-8bb1-54b1d50e3ecf"; $client_secret = $env:CS2; $resource = "https://graph.microsoft.com"; $authority = "https://login.microsoftonline.com/$tenant_id"; $tokenEndpointUri = "$authority/oauth2/token"; $content = "grant_type=client_credentials&client_id=$client_id &client_secret=$client_secret&resource=$resource"; $response = Invoke-WebRequest ` -Uri $tokenEndpointUri ` -Body $content ` -Method Post ` -UseBasicParsing

The small difference between this time and the previous blog is now we are using AppOnly permissions. The grant_type is client_credentials. And you don't need to supply username/password. The clientid/clientsecret pair is sufficient.

The whole thing $name = "group-name-here" $client_id = "44da3f20-fc0b-4f90-8bb1-54b1d50e3ecf"; $client_secret = $env:CS2; $tenant_id = "26e65220-5561-46ef-9783-ce5f20489241"; $resource = "https://graph.microsoft.com"; $authority = "https://login.microsoftonline.com/$tenant_id"; $tokenEndpointUri = "$authority/oauth2/token"; $content = "grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&resource=$resource"; $response = Invoke-WebRequest -Uri $tokenEndpointUri -Body $content -Method Post -UseBasicParsing $responseBody = $response.Content | ConvertFrom-JSON $responseBody $access_token = $responseBody.access_token # GET https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/group_list # $body = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/groups" -Headers @{"Authorization" = "Bearer $access_token"} # $body | ConvertTo-JSON # POST - this creates groups https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/group_post_groups $body = @{"displayName"= $name; "mailEnabled"=$false; "groupTypes"=@("Unified"); "securityEnabled"=$false; "mailNickname"=$name } | ConvertTo-Json $body = Invoke-RestMethod ` -Uri "https://graph.microsoft.com/v1.0/groups" ` -Headers @{"Authorization" = "Bearer $access_token"} ` -Body $body ` -ContentType "application/json" ` -Method POST $body | ConvertTo-JSON
AzureFunctions, PowerShell, MS Graph and AppOnly permission

There are no other dependencies - authenticate and calling MS Graph is really that simple - two web requests!

The default "Owner"

Usually, the user that created the group becomes the Owner of the group.

In the Delegate Permission example in the last blog, the group is created with my user account and I'm the owner.

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

主题: PowerShellHead
分页:12
转载请注明
本文标题:AzureFunctions, PowerShell, MS Graph and AppOnly permission
本站链接:http://www.codesec.net/view/532483.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(27)