C#/.Net8/MS Graph: how to call graphClient.Groups[groupId].GetAsync()?

I hope someone can point me to a working answer…:

I get a JwtSecurityToken with a list of groupIds and I need the names of the groups.
I already learned that I have to use MS Graph for this.
But by now all I get are different kinds of errors.
I’ve searched and tried so many examples, but I read there were many changes in these APIs, so older examples are not working any more, and I didn’t find a recent working one.

So here is my small test application:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public static void Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddAuthorization();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
WebApplication app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
// This call is just to prove that the Authorization is okay
app.MapGet("/Hi", () => "Hi, I'm secured :-)").WithOpenApi().RequireAuthorization();
app.MapGet("/GraphCallAsync", (HttpContext context) => GetGroupInfo(context, app)).WithOpenApi().RequireAuthorization();
app.Run();
}
</code>
<code> public static void Main(string[] args) { WebApplicationBuilder builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd")); builder.Services.AddAuthorization(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); WebApplication app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); // This call is just to prove that the Authorization is okay app.MapGet("/Hi", () => "Hi, I'm secured :-)").WithOpenApi().RequireAuthorization(); app.MapGet("/GraphCallAsync", (HttpContext context) => GetGroupInfo(context, app)).WithOpenApi().RequireAuthorization(); app.Run(); } </code>
 public static void Main(string[] args)
 {
     WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

     // Add services to the container.
     builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
         .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
     builder.Services.AddAuthorization();

     builder.Services.AddEndpointsApiExplorer();
     builder.Services.AddSwaggerGen();

     WebApplication app = builder.Build();

     // Configure the HTTP request pipeline.
     if (app.Environment.IsDevelopment())
     {
         app.UseSwagger();
         app.UseSwaggerUI();
     }

     app.UseHttpsRedirection();
     app.UseAuthentication();
     app.UseAuthorization();

     // This call is just to prove that the Authorization is okay
     app.MapGet("/Hi", () => "Hi, I'm secured :-)").WithOpenApi().RequireAuthorization();

     app.MapGet("/GraphCallAsync", (HttpContext context) => GetGroupInfo(context, app)).WithOpenApi().RequireAuthorization();

     app.Run();
 }

So I am trying to create a Graph Client, with many variations, but all of them lead to errors when I call graphClient.Groups, see the comments:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>static GraphServiceClient CreateGraphClient(WebApplication app)
{
string? tenantId = app.Configuration["AzureAd:TenantId"];
string? clientId = app.Configuration["AzureAd:ClientId"];
string? clientSecret = app.Configuration["AzureAd:ClientSecret"];
var scopes = new string[];
scopes = new string[] { "Group.Read.All" };
//=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed:
// AADSTS1002012: The provided value for scope Group.Read.All is not valid.
// Client credential flows must have a scope value with /.default suffixed to the resource identifier(application ID URI).
scopes = new string[] { "https://graph.microsoft.com/.default" }
//=> Microsoft.Graph.Models.ODataErrors.ODataError: Insufficient privileges to complete the operation.
scopes = new string[] { $"https://{clientId}/.default" };
//=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed:
// AADSTS500011: The resource principal named https://<** ClientId **> was not found in the tenant named xxxxxx.
// This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant.
//You might have sent your authentication request to the wrong tenant.
scopes = new string[] { "Group.Read.All", "https://graph.microsoft.com/.default" };
//=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed:
// AADSTS70011: The provided request must include a 'scope' input parameter.
// The provided value for the input parameter 'scope' is not valid.
// The scope Group.Read.All https://graph.microsoft.com/.default is not valid.
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
return graphClient;
}
</code>
<code>static GraphServiceClient CreateGraphClient(WebApplication app) { string? tenantId = app.Configuration["AzureAd:TenantId"]; string? clientId = app.Configuration["AzureAd:ClientId"]; string? clientSecret = app.Configuration["AzureAd:ClientSecret"]; var scopes = new string[]; scopes = new string[] { "Group.Read.All" }; //=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed: // AADSTS1002012: The provided value for scope Group.Read.All is not valid. // Client credential flows must have a scope value with /.default suffixed to the resource identifier(application ID URI). scopes = new string[] { "https://graph.microsoft.com/.default" } //=> Microsoft.Graph.Models.ODataErrors.ODataError: Insufficient privileges to complete the operation. scopes = new string[] { $"https://{clientId}/.default" }; //=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed: // AADSTS500011: The resource principal named https://<** ClientId **> was not found in the tenant named xxxxxx. // This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. //You might have sent your authentication request to the wrong tenant. scopes = new string[] { "Group.Read.All", "https://graph.microsoft.com/.default" }; //=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed: // AADSTS70011: The provided request must include a 'scope' input parameter. // The provided value for the input parameter 'scope' is not valid. // The scope Group.Read.All https://graph.microsoft.com/.default is not valid. var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret); var graphClient = new GraphServiceClient(clientSecretCredential, scopes); return graphClient; } </code>
static GraphServiceClient CreateGraphClient(WebApplication app)
{
    string? tenantId = app.Configuration["AzureAd:TenantId"];
    string? clientId = app.Configuration["AzureAd:ClientId"];
    string? clientSecret = app.Configuration["AzureAd:ClientSecret"];
    var scopes = new string[];

    scopes = new string[] { "Group.Read.All" };
    //=> Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed:
    //        AADSTS1002012: The provided value for scope Group.Read.All is not valid.
    //        Client credential flows must have a scope value with /.default suffixed to the resource identifier(application ID URI).

    scopes = new string[] { "https://graph.microsoft.com/.default" }
    //=>  Microsoft.Graph.Models.ODataErrors.ODataError: Insufficient privileges to complete the operation.


    scopes = new string[] { $"https://{clientId}/.default" };
    //=>  Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed:
    //        AADSTS500011: The resource principal named https://<** ClientId **> was not found in the tenant named xxxxxx. 
    //            This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. 
    //You might have sent your authentication request to the wrong tenant.

    scopes = new string[] { "Group.Read.All", "https://graph.microsoft.com/.default" };
    //=>  Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed:
    //        AADSTS70011: The provided request must include a 'scope' input parameter. 
    //    The provided value for the input parameter 'scope' is not valid.
    //    The scope Group.Read.All https://graph.microsoft.com/.default is not valid.

    var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);

    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
    return graphClient;
}

And in this function I put the call:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static async Task<IResult> GetGroupInfo(HttpContext context, WebApplication app)
{
Microsoft.Graph.GraphServiceClient graphClient = CreateGraphClient(app);
string groupId = "<*** a valid groupId, just for test ***>";
// this call yields the error
var singleGroup = await graphClient.Groups[groupId].GetAsync();
// more code to evaluate singleGroup.DisplayName in case the call would work
// with the parameter HttpContext I can analyze the token
</code>
<code>public static async Task<IResult> GetGroupInfo(HttpContext context, WebApplication app) { Microsoft.Graph.GraphServiceClient graphClient = CreateGraphClient(app); string groupId = "<*** a valid groupId, just for test ***>"; // this call yields the error var singleGroup = await graphClient.Groups[groupId].GetAsync(); // more code to evaluate singleGroup.DisplayName in case the call would work // with the parameter HttpContext I can analyze the token </code>
public static async Task<IResult> GetGroupInfo(HttpContext context, WebApplication app)
        {
            Microsoft.Graph.GraphServiceClient graphClient = CreateGraphClient(app);

            string groupId = "<*** a valid groupId, just for test ***>";

            // this call yields the error
            var singleGroup = await graphClient.Groups[groupId].GetAsync();

            // more code to evaluate singleGroup.DisplayName in case the call would work
            // with the parameter HttpContext I can analyze the token
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> }
</code>
<code> } </code>
    }

So, is the syntax of my scopes-array wrong or is there some other code or setting to change?
I am clueless, any help appreciated.

Thank you in advance,
Martin

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật