Forum Discussion
Creating SSO Application using Microsoft Graph
For modern development, you should use Microsoft.Graph v5. The code you have is written using the v5 SDK syntax.
Required NuGet Package:
- Microsoft.Graph - Version 5.0.0 or newer.
This single package will bring in all the necessary dependencies, including the models (Microsoft.Graph.Models) and the core client libraries needed to make the API calls.
Detailed Breakdown and Why Version Matters
The Microsoft Graph .NET SDK underwent a significant and breaking change between version 4.x and version 5.x. Your code is written using the newer, more streamlined syntax of v5.
Let's look at the specific lines in your code that confirm this:
- await graphClient.Applications.PostAsync(requestBody);
- In v5, the method to create a resource is a direct PostAsync call on the collection (.Applications). This is exactly what your code does.
- In the older v4, the syntax was more verbose: await graphClient.Applications.Request().AddAsync(requestBody);. Your code would not compile with the v4 SDK.
- await graphClient.Applications[createdApplication.Id].PatchAsync(updateRequestBody);
- In v5, accessing a specific resource by its ID is done using an indexer ([id]), followed by the PatchAsync method. This matches your code perfectly.
- In the older v4, this would have been await graphClient.Applications[createdApplication.Id].Request().UpdateAsync(updateRequestBody);.
- using Microsoft.Graph.Models;
- In v5, all the data models (like Application, WebApplication, etc.) were moved into a separate Microsoft.Graph.Models namespace, which is referenced at the top of your file.
- In v4, these models were in the base Microsoft.Graph namespace.
How to Add the Correct Package in Visual Studio
You can add the correct package using the NuGet Package Manager:
- Via Package Manager Console:
- (It's good practice to specify a recent version rather than just the minimum)
- powershell
- Install-Package Microsoft.Graph -Version 5.49.0
- Via NuGet Package Manager UI:
- Right-click on your project in the Solution Explorer and select "Manage NuGet Packages...".
- Go to the "Browse" tab.
- Search for Microsoft.Graph.
- Select the package and ensure the version is 5.0.0 or higher.
- Click "Install".
What About Authentication?
The code snippet you provided assumes you have already created and authenticated a GraphServiceClient instance named graphClient. To do that, you will also need an authentication library.
The recommended library to pair with the v5 SDK is:
- Azure.Identity
You would use it to create a credential object (like ClientSecretCredential or DeviceCodeCredential) which is then passed to the GraphServiceClient.
Example of creating the graphClient:
C#
using Azure.Identity;
using Microsoft.Graph;
// Tenant ID, Client ID, and Client Secret for your app registration
var tenantId = "YOUR_TENANT_ID";
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";
// The scopes your app needs. For managing applications, you need 'Application.ReadWrite.All'
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Create the credential object
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret );
// Create the GraphServiceClient
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// ... now your provided code will work ...
Summary:
To make your code work, you need to be using version 5.x of the Microsoft.Graph NuGet package. This version directly matches the syntax and structure of your two-step "create, then update" logic.