Configure Application Proxy using the Microsoft Graph API
Article
In this article, you'll learn how to configure Azure Active Directory (Azure AD) Application Proxy for an application. Application Proxy provides secure remote access and single sign-on to on-premises web applications. After configuring Application Proxy for an application, users can access their on-premises applications through an external URL, the My Apps portal, or other internal application portals.
Prerequisites
This tutorial assumes you have already installed a connector and completed the prerequisites for Application Proxy so that connectors can communicate with Azure AD services.
Sign in to an API client such as Graph Explorer, Postman, or create your own client app to call Microsoft Graph. To call Microsoft Graph APIs in this tutorial, you need to use an account with the global administrator role.
Grant yourself the following delegated permission: Directory.ReadWrite.All.
Note
The response objects shown might be shortened for readability.
Step 1: Create a custom application
To configure Application Proxy for an app using the API, you first create a custom application, and then update the application's onPremisesPublishing property to configure the App Proxy settings. In this tutorial, you use an application template to create an instance of a custom application and service principal in your tenant for management. The template ID for a custom application is 8adf8e6e-67b2-4cf2-a259-e3dc5476c621.
Record the id, appId, servicePrincipalId of the application to use later in the tutorial.
POST https://graph.microsoft.com/v1.0/applicationTemplates/8adf8e6e-67b2-4cf2-a259-e3dc5476c621/instantiate
Content-type: application/json
{
"displayName": "Contoso IWA App"
}
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.ApplicationTemplates.Item.Instantiate.InstantiatePostRequestBody
{
DisplayName = "Contoso IWA App",
};
var result = await graphClient.ApplicationTemplates["{applicationTemplate-id}"].Instantiate.PostAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new InstantiatePostRequestBody();
$requestBody->setDisplayName('Contoso IWA App');
$result = $graphServiceClient->applicationTemplates()->byApplicationTemplateId('applicationTemplate-id')->instantiate()->post($requestBody);
Insert the id that you recorded for the application into the URL to start the configuration of Application Proxy. In this example, you're using an app with the internal URL: https://contosoiwaapp.com. You also use the default domain for the external URL: https://contosoiwaapp-contoso.msappproxy.net. Update the following properties in the request body:
identifierUri, redirectUri, and homepageUrl - Set each to the same external URL.
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Application
{
IdentifierUris = new List<string>
{
"https://contosoiwaapp-contoso.msappproxy.net",
},
Web = new WebApplication
{
RedirectUris = new List<string>
{
"https://contosoiwaapp-contoso.msappproxy.net",
},
HomePageUrl = "https://contosoiwaapp-contoso.msappproxy.net",
},
};
var result = await graphClient.Applications["{application-id}"].PatchAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new Application();
$requestBody->setIdentifierUris(['https://contosoiwaapp-contoso.msappproxy.net', ]);
$web = new WebApplication();
$web->setRedirectUris(['https://contosoiwaapp-contoso.msappproxy.net', ]);
$web->setHomePageUrl('https://contosoiwaapp-contoso.msappproxy.net');
$requestBody->setWeb($web);
$result = $graphServiceClient->applications()->byApplicationId('application-id')->patch($requestBody);
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Application
{
OnPremisesPublishing = new OnPremisesPublishing
{
ExternalAuthenticationType = ExternalAuthenticationType.AadPreAuthentication,
InternalUrl = "https://contosoiwaapp.com",
ExternalUrl = "https://contosoiwaapp-contoso.msappproxy.net",
IsHttpOnlyCookieEnabled = true,
IsOnPremPublishingEnabled = true,
IsPersistentCookieEnabled = true,
IsSecureCookieEnabled = true,
IsStateSessionEnabled = true,
IsTranslateHostHeaderEnabled = true,
IsTranslateLinksInBodyEnabled = true,
},
};
var result = await graphClient.Applications["{application-id}"].PatchAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new Application();
$onPremisesPublishing = new OnPremisesPublishing();
$onPremisesPublishing->setExternalAuthenticationType(new ExternalAuthenticationType('aadpreauthentication'));
$onPremisesPublishing->setInternalUrl('https://contosoiwaapp.com');
$onPremisesPublishing->setExternalUrl('https://contosoiwaapp-contoso.msappproxy.net');
$onPremisesPublishing->setIsHttpOnlyCookieEnabled(true);
$onPremisesPublishing->setIsOnPremPublishingEnabled(true);
$onPremisesPublishing->setIsPersistentCookieEnabled(true);
$onPremisesPublishing->setIsSecureCookieEnabled(true);
$onPremisesPublishing->setIsStateSessionEnabled(true);
$onPremisesPublishing->setIsTranslateHostHeaderEnabled(true);
$onPremisesPublishing->setIsTranslateLinksInBodyEnabled(true);
$requestBody->setOnPremisesPublishing($onPremisesPublishing);
$result = $graphServiceClient->applications()->byApplicationId('application-id')->patch($requestBody);
GET https://graph.microsoft.com/beta/onPremisesPublishingProfiles/applicationProxy/connectors
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var result = await graphClient.OnPremisesPublishingProfiles["{onPremisesPublishingProfile-id}"].Connectors.GetAsync();
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$result = $graphServiceClient->onPremisesPublishingProfiles()->byOnPremisesPublishingProfileId('onPremisesPublishingProfile-id')->connectors()->get();
For this example, a new connectorGroup is created named IWA Demo Connector Group that is used for the application. Record the id that is returned to use in the next step.
POST https://graph.microsoft.com/beta/onPremisesPublishingProfiles/applicationProxy/connectors/f2cab422-a1c8-4d70-a47e-2cb297a2e051/memberOf/$ref
Content-type: application/json
{
"@odata.id":"https://graph.microsoft.com/beta/onPremisesPublishingProfiles/applicationProxy/connectorGroups/3e6f4c35-a04b-4d03-b98a-66fff89b72e6"
}
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Beta.Models.ReferenceCreate
{
OdataId = "https://graph.microsoft.com/beta/onPremisesPublishingProfiles/applicationProxy/connectorGroups/3e6f4c35-a04b-4d03-b98a-66fff89b72e6",
};
await graphClient.OnPremisesPublishingProfiles["{onPremisesPublishingProfile-id}"].Connectors["{connector-id}"].MemberOf.Ref.PostAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new ReferenceCreate();
$requestBody->set@odataid('https://graph.microsoft.com/beta/onPremisesPublishingProfiles/applicationProxy/connectorGroups/3e6f4c35-a04b-4d03-b98a-66fff89b72e6');
$graphServiceClient->onPremisesPublishingProfiles()->byOnPremisesPublishingProfileId('onPremisesPublishingProfile-id')->connectors()->byConnectorId('connector-id')->memberOf()->ref()->post($requestBody);
PUT https://graph.microsoft.com/beta/applications/bf21f7e9-9d25-4da2-82ab-7fdd85049f83/connectorGroup/$ref
Content-type: application/json
{
"@odata.id":"https://graph.microsoft.com/onPremisesPublishingProfiles/applicationproxy/connectorGroups/3e6f4c35-a04b-4d03-b98a-66fff89b72e6"
}
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Beta.Models.ReferenceUpdate
{
OdataId = "https://graph.microsoft.com/onPremisesPublishingProfiles/applicationproxy/connectorGroups/3e6f4c35-a04b-4d03-b98a-66fff89b72e6",
};
await graphClient.Applications["{application-id}"].ConnectorGroup.Ref.PutAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new ReferenceUpdate();
$requestBody->set@odataid('https://graph.microsoft.com/onPremisesPublishingProfiles/applicationproxy/connectorGroups/3e6f4c35-a04b-4d03-b98a-66fff89b72e6');
$graphServiceClient->applications()->byApplicationId('application-id')->connectorGroup()->ref()->put($requestBody);
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Beta.Applications.Item.Application
{
AdditionalData = new Dictionary<string, object>
{
{
"onPremisesPublishing" , new
{
SingleSignOnSettings = new
{
KerberosSignOnSettings = new
{
KerberosServicePrincipalName = "HTTP/iwademo.contoso.com",
KerberosSignOnMappingAttributeType = "userPrincipalName",
},
SingleSignOnMode = "onPremisesKerberos",
},
}
},
},
};
await graphClient.Applications["{application-id}"].PatchAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new Application();
$additionalData = [
'onPremisesPublishing' => $requestBody = new OnPremisesPublishing();
$singleSignOnSettings = new SingleSignOnSettings();
$singleSignOnSettingsKerberosSignOnSettings = new KerberosSignOnSettings();
$ singleSignOnSettingsKerberosSignOnSettings->setKerberosServicePrincipalName('HTTP/iwademo.contoso.com');
$ singleSignOnSettingsKerberosSignOnSettings->setKerberosSignOnMappingAttributeType('userPrincipalName');
$singleSignOnSettings->setKerberosSignOnSettings($singleSignOnSettingsKerberosSignOnSettings);
$ singleSignOnSettings->setSingleSignOnMode('onPremisesKerberos');
$requestBody->setSingleSignOnSettings($singleSignOnSettings);
$requestBody->setOnPremisesPublishing($onPremisesPublishing);
];
$requestBody->setAdditionalData($additionalData);
$graphServiceClient->applications()->byApplicationId('application-id')->patch($requestBody);
For this tutorial, you create a user account that is assigned to the app role. In the request body, change contoso.com to the domain name of your tenant. You can find tenant information on the Azure Active Directory overview page. Record the id of the user account to be used in the next step.
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new User
{
AccountEnabled = true,
DisplayName = "MyTestUser1",
MailNickname = "MyTestUser1",
UserPrincipalName = "MyTestUser1@contoso.com",
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = true,
Password = "Contoso1234",
},
};
var result = await graphClient.Users.PostAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new User();
$requestBody->setAccountEnabled(true);
$requestBody->setDisplayName('MyTestUser1');
$requestBody->setMailNickname('MyTestUser1');
$requestBody->setUserPrincipalName('MyTestUser1@contoso.com');
$passwordProfile = new PasswordProfile();
$passwordProfile->setForceChangePasswordNextSignIn(true);
$passwordProfile->setPassword('Contoso1234');
$requestBody->setPasswordProfile($passwordProfile);
$result = $graphServiceClient->users()->post($requestBody);
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new Microsoft.Graph.Beta.ServicePrincipals.Item.AppRoleAssignments.AppRoleAssignmentsPostRequestBody
{
AdditionalData = new Dictionary<string, object>
{
{
"principalId" , "4628e7df-dff3-407c-a08f-75f08c0806dc"
},
{
"principalType" , "User"
},
{
"appRoleId" , "18d14569-c3bd-439b-9a66-3a2aee01d14f"
},
{
"resourceId" , "a8cac399-cde5-4516-a674-819503c61313"
},
},
};
await graphClient.ServicePrincipals["{servicePrincipal-id}"].AppRoleAssignments.PostAsync(requestBody);
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$requestBody = new AppRoleAssignmentsPostRequestBody();
$additionalData = [
'principalId' => '4628e7df-dff3-407c-a08f-75f08c0806dc',
'principalType' => 'User',
'appRoleId' => '18d14569-c3bd-439b-9a66-3a2aee01d14f',
'resourceId' => 'a8cac399-cde5-4516-a674-819503c61313',
];
$requestBody->setAdditionalData($additionalData);
$graphServiceClient->servicePrincipals()->byServicePrincipalId('servicePrincipal-id')->appRoleAssignments()->post($requestBody);
Test the application by visiting the External URL configured for the app on your browser and then sign in with your test user. You should be able to log into the app and access the application.
Step 7: Clean up resources
The resources that you created in this tutorial are not intended to be used in a production environment. In this step, you remove the resources that you created.
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
await graphClient.Users["{user-id}"].DeleteAsync();
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$graphServiceClient->users()->byUserId('user-id')->delete();
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
await graphClient.Applications["{application-id}"].DeleteAsync();
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$graphServiceClient->applications()->byApplicationId('application-id')->delete();
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
await graphClient.OnPremisesPublishingProfiles["{onPremisesPublishingProfile-id}"].ConnectorGroups["{connectorGroup-id}"].DeleteAsync();
<?php
// THIS SNIPPET IS A PREVIEW FOR THE KIOTA BASED SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($requestAdapter);
$graphServiceClient->onPremisesPublishingProfiles()->byOnPremisesPublishingProfileId('onPremisesPublishingProfile-id')->connectorGroups()->byConnectorGroupId('connectorGroup-id')->delete();