Я следую этому руководству о том, как запустить Microsoft Graph API с функциями Azure:
Руководство по функциям Azure
Я четко следовал инструкциям и вставьте этот код в свой проект:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Security.Claims;
using Azure.Identity;
using Microsoft.Graph;
namespace AZFunction1
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log, ClaimsPrincipal claimIdentity)
{
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
Constants.TenantId, Constants.AppId, Constants.ClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, Constants.scopes);
var user = await graphClient.Users[claimIdentity.Identity.Name].GetAsync();
var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
return new OkObjectResult(json);
}
}
}
Однако в этом коде появляется ошибка:graphClient.Users[claimIdentity.Identity.Name]
CS0021: Cannot применить индексацию с помощью [] к выражению типа «Объект»
Может кто-нибудь мне помочь?
Любая помощь будет очень признательна.
Любая помощь будет очень признательна.
p>
Я пробовал использовать ChatGPT, который предлагает мне использовать следующий код, но ошибка остается прежней:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Security.Claims;
using Azure.Identity;
using Microsoft.Graph;
namespace AZFunction1
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log, ClaimsPrincipal claimIdentity)
{
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
Constants.TenantId, Constants.AppId, Constants.ClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, Constants.scopes);
// Extract user ID from ClaimsPrincipal
var userId = claimIdentity?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
// Log the user ID
log.LogInformation($"User ID: {userId}");
if (string.IsNullOrEmpty(userId))
{
log.LogError("User identifier is null or empty.");
return new BadRequestObjectResult("Invalid user identifier.");
}
try
{
var user = await graphClient.Users[userId].Request().GetAsync();
var json = JsonConvert.SerializeObject(user);
return new OkObjectResult(json);
}
catch (ServiceException ex)
{
log.LogError($"Error fetching user: {ex.Message}");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... ft-graph-a
Код ошибки CS:0021 в проекте функции Azure в VS для запуска API Microsoft Graph с функциями Azure. ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение