This should be possible with Graph (CoPilot):
using Microsoft.Graph;
using Microsoft.Identity.Client;
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";
var tenantId = "YOUR_TENANT_ID";
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var user = new User
{
AccountEnabled = true,
DisplayName = "John Doe",
MailNickname = "johndoe",
UserPrincipalName = "johndoe@yourtenant.onmicrosoft.com",
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = true,
Password = "TempP@ssw0rd!"
}
};
await graphClient.Users
.Request()
.AddAsync(user);
But don't require a password change!