Umstellung der Anwendung von lokalen JSON-Daten auf Live-Daten aus Azure Active Directory (Microsoft 365) mithilfe der Microsoft Graph API.
Dank Repository Pattern sind keine Änderungen am Frontend oder an den Controllern notwendig.
Voraussetzung: Zugriff auf das Azure Portal (Microsoft Entra ID).
- App Registration erstellen
- Client Secret erzeugen
- API-Berechtigung hinzufügen:
User.Read.All(Application)
- Admin Consent erteilen
- Notieren:
- TenantId
- ClientId
- ClientSecret
Eintrag in WebAPI/appsettings.json:
{
"AzureAd": {
"TenantId": "IHRE-GUID-HIER",
"ClientId": "IHRE-GUID-HIER",
"ClientSecret": "IHR-SECRET-HIER"
}
}Im Projekt WebAPI:
Microsoft.GraphAzure.Identity
Datei: WebAPI/Services/GraphUserService.cs
using Application;
using Microsoft.Graph;
using Microsoft.Graph.Models;
namespace WebAPI.Services;
public class GraphUserService : IUserService
{
private readonly GraphServiceClient _graphClient;
public GraphUserService(GraphServiceClient graphClient)
{
_graphClient = graphClient;
}
public async Task<List<Application.User>> SearchAsync(string term)
{
if (string.IsNullOrWhiteSpace(term)) return new();
var result = await _graphClient.Users.GetAsync(config =>
{
config.QueryParameters.Filter =
$"startswith(displayName, '{term}') or startswith(mail, '{term}')";
config.QueryParameters.Select = new[]
{
"id", "displayName", "mail", "mobilePhone",
"jobTitle", "department", "officeLocation", "employeeId"
};
config.QueryParameters.Top = 10;
});
return result?.Value?.Select(MapToAppUser).ToList() ?? new();
}
public async Task<Application.User?> GetByIdAsync(string id)
{
var user = await _graphClient.Users[id].GetAsync(config =>
{
config.QueryParameters.Select = new[]
{
"id", "displayName", "mail", "mobilePhone",
"jobTitle", "department", "officeLocation", "employeeId"
};
});
return user != null ? MapToAppUser(user) : null;
}
private Application.User MapToAppUser(Microsoft.Graph.Models.User graphUser)
{
return new Application.User
{
Id = graphUser.Id ?? string.Empty,
Name = graphUser.DisplayName ?? "Unbekannt",
Email = graphUser.Mail ?? string.Empty,
Phone = graphUser.MobilePhone ?? string.Empty,
Role = graphUser.JobTitle ?? "N/A",
Department = graphUser.Department ?? string.Empty,
Faculty = graphUser.OfficeLocation ?? string.Empty,
IdentificationNumber = graphUser.EmployeeId ?? "N/A"
};
}
}Using-Statements:
using Microsoft.Graph;
using Azure.Identity;Graph Client registrieren (vor AddControllers):
builder.Services.AddScoped<GraphServiceClient>(sp =>
{
var settings = builder.Configuration.GetSection("AzureAd");
var credential = new ClientSecretCredential(
settings["TenantId"],
settings["ClientId"],
settings["ClientSecret"]);
return new GraphServiceClient(credential);
});Service austauschen:
// Alt
builder.Services.AddScoped<IUserService, FileUserService>();
// Neu
builder.Services.AddScoped<IUserService, GraphUserService>();Nach dem Neustart der WebAPI werden die Benutzerdaten direkt aus Azure Active Directory geladen.
✔ Frontend unverändert ✔ Controller unverändert ✔ Datenquelle flexibel austauschbar