I have been struggling with a CS1061 error for '.Request()' and cannot seem to figure it out, if anyone can think of a solution or a work around it would be greatly appreciated!
The program is running .NET 8 and using Microsoft.graph, microsoft.graph.core and Azure.Identity packages, all up to date.
static void UpdateUser()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "...";
var clientId = "...";
var clientSecret = "...";
ClientSecretCredential clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
Console.WriteLine("Updating a user...");
Console.Write("Enter the Object ID of the user to update: ");
string objectId = Console.ReadLine();
try
{
var user = graphClient.Users[objectId].GetAsync().Result;
// Display current user attributes
Console.WriteLine($"Current attributes of the user with Object ID {objectId}:");
Console.WriteLine($"Display Name: {user.DisplayName}");
Console.WriteLine($"Given Name: {user.GivenName}");
Console.WriteLine($"Surname: {user.Surname}");
Console.WriteLine($"Email Address: {user.Identities[0].IssuerAssignedId}");
Console.WriteLine($"Business Phone Number: {user.BusinessPhones?[0]}");
// Prompt the user to update attributes
Console.WriteLine("Enter new values for the user's attributes (press Enter to keep current value):");
Console.Write("New Display Name: ");
string newDisplayName = Console.ReadLine();
Console.Write("New Given Name: ");
string newGivenName = Console.ReadLine();
Console.Write("New Surname: ");
string newSurname = Console.ReadLine();
Console.Write("New Email Address: ");
string newEmailAddress = Console.ReadLine();
Console.Write("New Business Phone Number: ");
string newBusinessPhone = Console.ReadLine();
// Update user attributes
if (!string.IsNullOrEmpty(newDisplayName))
user.DisplayName = newDisplayName;
if (!string.IsNullOrEmpty(newGivenName))
user.GivenName = newGivenName;
if (!string.IsNullOrEmpty(newSurname))
user.Surname = newSurname;
if (!string.IsNullOrEmpty(newEmailAddress))
user.Identities[0].IssuerAssignedId = newEmailAddress;
if (!string.IsNullOrEmpty(newBusinessPhone))
{
if (user.BusinessPhones == null)
user.BusinessPhones = new List<string>();
user.BusinessPhones.Clear();
user.BusinessPhones.Add(newBusinessPhone);
}
// Update the user in Azure AD B2C
graphClient.Users[objectId].Request().UpdateAsync(user).Wait();
Console.WriteLine("User updated successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error updating user: {ex.Message}");
}
}
Hopefully this information is suffice, if there is any other info I can provide that may help feel free to ask (I am fairly new to coding so good chance I have missed something!).
Thanks in advance.
I have tried rebuild and cleaning the program, I also asked chatGPT if it has any ideas why this may not work, but it could not provide a fix. All packages have been updated.
Initially, I registered one application and granted User.ReadWrite.All
permission of Application type as below:
To update the users in Azure AD B2C, you can make use of below modified code:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
using System;
class Program
{
static void Main(string[] args)
{
UpdateUser().Wait();
}
static async Task UpdateUser()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "appId";
var tenantId = "tenantId";
var clientSecret = "secret";
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
Console.WriteLine("Updating a user...");
Console.Write("Enter the Object ID of the user to update: ");
string objectId = Console.ReadLine();
try
{
var user = await graphClient.Users[objectId].GetAsync();
// Display current user attributes
Console.WriteLine($"Current attributes of the user with Object ID {objectId}:");
Console.WriteLine($"Display Name: {user.DisplayName}");
Console.WriteLine($"Given Name: {user.GivenName}");
Console.WriteLine($"Surname: {user.Surname}");
Console.WriteLine($"Email Address: {user.Mail}");
Console.WriteLine($"Business Phone Number: {user.BusinessPhones?[0]}");
// Prompt the user to update attributes
Console.WriteLine("Enter new values for the user's attributes (press Enter to keep current value):");
Console.Write("New Display Name: ");
string newDisplayName = Console.ReadLine();
Console.Write("New Given Name: ");
string newGivenName = Console.ReadLine();
Console.Write("New Surname: ");
string newSurname = Console.ReadLine();
Console.Write("New Email Address: ");
string newEmailAddress = Console.ReadLine();
Console.Write("New Business Phone Number: ");
string newBusinessPhone = Console.ReadLine();
// Update user attributes
if (!string.IsNullOrEmpty(newDisplayName))
user.DisplayName = newDisplayName;
if (!string.IsNullOrEmpty(newGivenName))
user.GivenName = newGivenName;
if (!string.IsNullOrEmpty(newSurname))
user.Surname = newSurname;
if (!string.IsNullOrEmpty(newEmailAddress))
user.Mail = newEmailAddress;
if (!string.IsNullOrEmpty(newBusinessPhone))
{
if (user.BusinessPhones == null)
user.BusinessPhones = new List<string>();
user.BusinessPhones.Clear();
user.BusinessPhones.Add(newBusinessPhone);
}
// Update the user in Azure AD B2C
await graphClient.Users[objectId].PatchAsync(user);
Console.WriteLine("User updated successfully");
}
catch (Exception ex)
{
Console.WriteLine($"Error updating user: {ex.Message}");
}
}
}
Response:
Reference:
Upgrade-to-v5.0 · microsoftgraph/msgraph-sdk-dotnet changes · GitHub