In my ASP.net Core MVC 8 application, I am using Microsoft Graph to get a list of members of an Azure Active Directory group. The authentication used is Microsoft Entra. How come when I try to call the Graph API in my controller, this error gets thrown? System.InvalidOperationException: ‘Unable to perform redirect as Location Header is not set in response’
How can I set the Location Header?
public async Task<dynamic> GetGroupMembers(string groupId)
{
try
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzureGovernment
};
var clientSecretCredential = new ClientSecretCredential(configuration["TenantId"], configuration["ClientId"], configuration["ClientSecret"], options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// this is where the error gets thrown
var members = await graphClient.Groups[groupId].Members.GetAsync();
}
catch (Exception ex)
{
}
return null;
}
So far I have used Microsoft’s documentation on how to create a graph client using the Tenant ID, Client ID, and Client Secret provided by my security team. I have also set my client credential to use the Azure Government host. (Source: [https://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=csharp](Microsoft Graph Client))
The group ID I am trying to get members of is the application’s assigned group, which was set by security in the Azure Portal. I am able to get the group’s ID through my claims since I am a member of the group.