Я пытаюсь создать конечные точки REST для создания пользователей в Azure Entra (AD) и проверки того, присутствует ли пользователь в группе или нет. Эти конечные точки будут вызываться из пользовательских интерфейсных приложений.
Я попробовал следующий код, но, к сожалению, столкнулся со следующей ошибкой:
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Identity.Client;
using Azure.Identity;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace Entra_SPIKE.Controllers
{
[ApiController]
[Route("[controller]")]
public class CreateUserController : Controller
{
private GraphServiceClient _graphServiceClient;
public CreateUserController(GraphServiceClient graphServiceClient)
{
_graphServiceClient = graphServiceClient;
}
[HttpPost(Name = "PostCreateUser")]
public async Task CreateUser([FromBody] CreateUserRequest request)
{
try
{
var user = new User
{
AccountEnabled = request.AccountEnabled,
DisplayName = request.DisplayName,
MailNickname = request.MailNickname,
UserPrincipalName = request.UserPrincipalName ,
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = request.ForceChangePasswordNextSignIn,
Password = request.Password
}
};
// The client credentials flow requires that you request the
// /.default scope, and pre-configure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Values from app registration
var tenantId = "MY-ID";
// Values from app registration
var clientId = "MY-ID";
var clientSecret = "MY-Password";
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphclient = new GraphServiceClient(clientSecretCredential, scopes);
var result = await graphclient.Users.PostAsync(user);
return Ok("User created successfully");
}
catch (Exception ex)
{
return StatusCode(500, $"An error occurred: {ex.Message}");
}
}
}
public class CreateUserRequest
{
public string DisplayName { get; set; }
public string MailNickname { get; set; }
public string UserPrincipalName { get; set; }
public string Password { get; set; }
public bool AccountEnabled { get; set; }
public bool ForceChangePasswordNextSignIn { get; set; }
}
}
'Microsoft.Graph.GraphServiceClient' while attempting to activate
'Entra_SPIKE.Controllers.CreateUserController'. at
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
...
Could anyone please guide me on this?
Источник: https://stackoverflow.com/questions/781 ... ot-working
Мобильная версия