A cloud-based identity and access management service for securing user authentication and resource access
Hi All,
Finally I setup working code, thank you very much for your help. you can check below full code for reference :
<policies>
<inbound>
<base />
<!-- Step 1: Initialize Group Name and User Email -->
<set-variable name="groupName" value="@(context.Request.OriginalUrl.Query.GetValueOrDefault("group_name"))" />
<!-- set-variable name="userEmail" value="@{
var userEmailValue = context.Request.Url.Query.GetValueOrDefault("user_email", "");
return userEmailValue;
}" / -->
<set-variable name="userEmail" value="@(context.Request.OriginalUrl.Query.GetValueOrDefault("user_email"))" />
<!--set-variable name="groupName" value="@(context.Request.MatchedParameters["group_name"])" />
<set-variable name="userEmail" value="@(context.Request.MatchedParameters["user_email"])" /-->
<!-- Step 2: Acquire Microsoft Graph Token -->
<send-request mode="new" response-variable-name="tokenResponse" timeout="20">
<set-url>https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/x-www-form-urlencoded</value>
</set-header>
<set-body>@("client_id={client-id}&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret={client-secret}&grant_type=client_credentials")</set-body>
</send-request>
<!--Store The Token in "access_token" variable-->
<set-variable name="access_token" value="@(((IResponse)context.Variables["tokenResponse"]).Body.As<JObject>()["access_token"].ToString())" />
<!-- Step 3: Get Group ID by Name -->
<send-request mode="new" response-variable-name="groupResponse" timeout="20">
<set-url>@("https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '" + context.Variables.GetValueOrDefault<string>("groupName") + "'")</set-url>
<set-method>GET</set-method>
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + context.Variables["access_token"])</value>
</set-header>
</send-request>
<!--Store The Group ID in "groupId" variable-->
<set-variable name="groupId" value="@(((IResponse)context.Variables["groupResponse"]).Body.As<JObject>()["value"]?[0]?["id"])" />
<!-- Step 4: Get User ID by Email -->
<send-request mode="new" response-variable-name="userResponse" timeout="20">
<set-url>@("https://graph.microsoft.com/v1.0/users/" + context.Variables.GetValueOrDefault<string>("userEmail"))</set-url>
<set-method>GET</set-method>
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + context.Variables["access_token"])</value>
</set-header>
</send-request>
<!--Store The User ID in "userId" variable-->
<set-variable name="userId" value="@(((IResponse)context.Variables["userResponse"]).Body.As<JObject>()?["id"])" />
<!-- Step 5: Add User to Group-->
<set-method>POST</set-method>
<set-header name="Authorization" exists-action="override">
<value>@("Bearer " + context.Variables["access_token"])</value>
</set-header>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@("{\"@odata.id\": \"https://graph.microsoft.com/v1.0/directoryObjects/" + context.Variables["userId"] + "\"}")</set-body>
<rewrite-uri template="@("/v1.0/groups/" + context.Variables["groupId"] + "/members/$ref")" copy-unmatched-params="false" />
<set-backend-service base-url="https://graph.microsoft.com" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<choose>
<when condition="@(context.Response.StatusCode == 204)">
<return-response>
<set-status code="200" reason="OK" />
<set-body>{ "message": "User Added Successfully." }</set-body>
</return-response>
</when>
<when condition="@(context.Response.StatusCode == 400)">
<return-response>
<set-status code="200" reason="OK" />
<set-body>{ "message": "Bad Request. User Already Member of The Group Or Invalid User Email." }</set-body>
</return-response>
</when>
<when condition="@(context.Response.StatusCode == 500)">
<return-response>
<set-status code="200" reason="OK" />
<set-body>{ "message": "Bad Request. Invalid Group Name." }</set-body>
</return-response>
</when>
</choose>
</outbound>
<on-error>
<base />
</on-error>
</policies>