I have developed a web application that successfully executes PowerShell scripts on my local machine. However, upon deploying the application to Azure App Service, I encountered errors indicating that the PowerShell commands were not being executed. Despite my efforts, I couldn't find any C# code examples for creating an App registration and adding permissions programmatically.
Scenario 1: Create App registration in Microsoft Entra ID(AAD).
Scenario 2: Update Permissions, Authentication type, Adding Owners into above created App registration.
In essence, my objective is to accomplish the following tasks:
- Execute PowerShell scripts within the web application.
- Ensure that these PowerShell commands function correctly when the application is deployed to Azure App Service.
- Investigate potential alternatives, such as utilizing C# code, to programmatically create an App registration and add permissions.
Below Script used to create App registration
$appName = $PartnerName+$PartnerAppSuffix
$redirectUris = @("https://mscloud.onmicrosoft.com/$appName")
$secretName = "secretKey"
$App = New-AzADApplication -DisplayName $appName `
-ReplyUrls $redirectUris `
-Homepage "https://mscloud.onmicrosoft.com/$appName" `
-IdentifierUris "https://mscloud.onmicrosoft.com/$appName" `
-Web @{
ImplicitGrantSetting = @{
EnableAccessTokenIssuance = $true
EnableIdTokenIssuance = $true
}
}
# Create a new service principal for the app
$ServicePrincipal = New-AzADServicePrincipal -ApplicationId $App.AppId
I'm in search of C# code snippets that facilitate the creation of App registrations and the addition of permissions and owners programmatically.
I agree with @Tiny Wang, you can make use of Microsoft Graph API to create the application and assign permissions:
The Microsoft Entra ID application created successfully with the permissions like below:
Note that: The application you are using to authenticate must be granted with
Application.ReadWrite.Allapplication type permission.user.readpermission to create the Service Principal.Reference:
Create application - Microsoft Graph v1.0 | Microsoft