Cristian
Cristian

Reputation: 111

Updating Azure AD Application via C#

I have an already existing Azure AD Application. I would like to be able to update it via C# code, just like I do via the Azure CLI. All I need is to add two new reply URLs to the application.

With the Azure CLI I use:

az ad app update --id <my_app_id> --reply-urls <url_1> <url_2>

How can I get the equivalent but within C# code? I've found that the Azure SDK does not provide such a functionality, or at least I couldn't find it. I have a Service Principal which I want this operation done through.

Upvotes: 0

Views: 400

Answers (2)

Cristian
Cristian

Reputation: 111

Here's one solution I came up with, turns out you can use System.Diagnostics.Process to invoke CMD.exe via C# and use the cli through there:

string strCmdText;
            strCmdText = "/C az login --service-principal -u <client_id> -p <client_secret> --tenant <tenant_id>";
            System.Diagnostics.Process.Start("CMD.exe", strCmdText);
            System.Diagnostics.Process.Start("CMD.exe", "/C az ad app update --id <app_id> --reply-urls https://testfromcsharp.com/");

and it worked.

EDIT: The best solution was to use the Microsoft Graph SDK within my .NET project and authenticate/invoke functions through it. https://learn.microsoft.com/en-us/graph/sdks/sdks-overview

Upvotes: 1

Tiny Wang
Tiny Wang

Reputation: 16146

You can use this graph api to update your azure ad appliction. And pls note that the application id used in the request url is the object id of the azure ad application but not the application id.

enter image description here

You can first call Get https://graph.microsoft.com/v1.0/applications/object_id_here first to get the current web claim of this app, that should be similar to

{
    "web": {
        "homePageUrl": null,
        "logoutUrl": null,
        "redirectUris": [
            "http://localhost:3000"
        ],
        "implicitGrantSettings": {
            "enableAccessTokenIssuance": true,
            "enableIdTokenIssuance": true
        }
    }
}

Then you modify the redirectUris property with all the urls you wanna set to the application, pls note this update will cover all the original redirect urls. Then copy the json content and call the PATCH request to update the api.

enter image description here

Upvotes: 1

Related Questions