I'm trying to create my app registration (Application) and enterprise application (ServicePrincipal) from code. But, though the service principal is created, it does not show when I go to Enterprise Applications in de AAD admin center.

Below is the code that I use to create the application and service principal. I have added the User.Read permission to the application as I found suggestions that said a ServicePrincipal would not show as a Enterprise Application unless the application had a permission.

I have set the ServicePrincipalType to Application as, according to the documentation, that seems to be the correct type. I have verified that the type of a ServicePrincipal that does show up in 'Enterprise Applications' is set to Application.

var microsoftGraphAppId = "00000003-0000-0000-c000-000000000000";

var microsoftGraphServicePrinciple = _graphClient.ServicePrincipals.Request().Filter($"appId eq '{microsoftGraphAppId}'").GetAsync().Result.First();

var user_read_id = microsoftGraphServicePrinciple.Oauth2PermissionScopes.First(p => p.Value == "User.Read").Id;

var newApplication = new Application
{
    DisplayName = $"TestApp - {DateTime.Now.ToShortTimeString()}",
    SignInAudience = "AzureADMyOrg",
    RequiredResourceAccess = new List<RequiredResourceAccess>
    {
        new RequiredResourceAccess
        {
            ResourceAppId = microsoftGraphAppId,
            ResourceAccess = new List<ResourceAccess>
            {
                new ResourceAccess
                {
                    Id = user_read_id,
                    Type = "Scope"
                }
            }
        }
    },
};
var application = _graphClient.Applications.Request().AddAsync(newApplication).Result;

var newServicePrincipal = new ServicePrincipal
{
    AppId = application.AppId,
    ServicePrincipalType = "Application",
};
var servicePrincipal = _graphClient.ServicePrincipals.Request().AddAsync(newServicePrincipal).Result;
1

There are 1 best solutions below

2
prinkpan On BEST ANSWER

You need to add a tag with value WindowsAzureActiveDirectoryIntegratedApp according to this documentation

So your code should be (please correct the syntax for tags as needed. I haven't tried it in VS)

var newServicePrincipal = new ServicePrincipal
{
    AppId = application.AppId,
    ServicePrincipalType = "Application",
    Tags = new [] {"WindowsAzureActiveDirectoryIntegratedApp"}
};