Reputation: 31
I am trying to write simple .net console to create Azure AD group using Graph API with the below code, however the code is not returning any error message and also when i try to run the code the group is not getting created.
What is the thing which i am doing wrong.
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using System;
using System.Collections.Generic;
namespace AADConsole2
{
class Program
{
private const string tenantId = "<<tenantid>>";
private const string clientId = "<<client id>>";
private static string appKey = "<<client secret>>";
static void Main(string[] args)
{
CreateADGroup();
}
public static async void CreateADGroup()
{
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(appKey)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var testGroup = new Group
{
Description = "testgroupdescription",
DisplayName = "testgroupkk",
GroupTypes = new List<String>()
{},
MailEnabled = false,
MailNickname = "testnickname",
SecurityEnabled = true,
//AdditionalData = additionalDataGroupB
};
await graphClient.Groups.Request().AddAsync(testGroup);
}
}
}
Upvotes: 0
Views: 179
Reputation: 9569
I tested your code, and although no errors were reported, it was indeed impossible to create a group. Because your code does not seem to be able to obtain the token, and you have not set the scope
in the code.
I wrote the test code using the console application, and the local test can perfectly create the group. You can try it:
using System;
using Microsoft.Identity.Client;
using Microsoft.Graph.Auth;
using Microsoft.Graph;
using System.Collections.Generic;
namespace test
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
IConfidentialClientApplication app;
app = ConfidentialClientApplicationBuilder.Create("{client id}")
.WithClientSecret("{Client Secret}")
.WithAuthority(new Uri("https://login.microsoftonline.com/{tenant}"))
.Build();
AuthenticationResult result = null;
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
string accesstoken = result.AccessToken;
/*Console.WriteLine(accesstoken);*/
ClientCredentialProvider authProvider = new ClientCredentialProvider(app);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var testGroup = new Group
{
Description = "testgroupdescription",
DisplayName = "testgroup1",
GroupTypes = new List<String>()
{ },
MailEnabled = false,
MailNickname = "testnickname",
SecurityEnabled = true,
//AdditionalData = additionalDataGroupB
};
await graphClient.Groups.Request().AddAsync(testGroup);
}
}
}
Upvotes: 1