Код: Выделить всё
using ARMS_API.CloudStorage;
using Azure.Identity;
using Box.V2.Config;
using Box.V2.JWTAuth;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Microsoft.Graph;
using Microsoft.Graph.Drives.Item.Items.Item.CreateUploadSession;
using Microsoft.Graph.Models;
namespace ARMS_API.Azure
{
public class AzureServices : IAzureServices
{
protected IConfiguration _configuration;
public AzureServices(IConfiguration configuration)
{
_configuration = configuration;
}
public GraphServiceClient GetGraphClient()
{
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
var chainedTokenCredential = GetChainedTokenCredentials();
return new GraphServiceClient(chainedTokenCredential, scopes);
}
private ChainedTokenCredential GetChainedTokenCredentials()
{
var tenantId = _configuration["AzureAd:TenantId"]!;
var clientId = _configuration["AzureAd:ClientId"]!;
var clientSecret = _configuration["AzureAd:ClientSecret"]!;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var devClientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var chainedTokenCredential = new ChainedTokenCredential(devClientSecretCredential);
return chainedTokenCredential;
}
}
}
Код: Выделить всё
builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes: new string[] { "https://graph.microsoft.com/.default" })
.AddDistributedTokenCaches();
Подробнее здесь: https://stackoverflow.com/questions/784 ... tion-based
Мобильная версия