无法在启动配置服务 .Net 5.0 中添加 AddMicrosoftGraph

Pra*_*ale 6 microsoft-graph-api

我在我的项目中添加了 Nuget 包 MicrosoftGraph 3.24.0,并且AddMicrosoftGraph启动配置服务中的调用出现以下错误。

为什么?

错误

“MicrosoftIdentityAppCallsWebApiAuthenticationBuilder”不包含“AddMicrosoftGraph”的定义,并且找不到接受“MicrosoftIdentityAppCallsWebApiAuthenticationBuilder”类型的第一个参数的可访问扩展方法“AddMicrosoftGraph”(您是否缺少 using 指令或程序集引用?)

use*_*152 20

要在 ASP.NET Core 项目中使用 Graph API,您应该添加 nuget Microsoft.Identity.Web.MicrosoftGraph(对于 v1.0)或Microsoft.Identity.Web.MicrosoftGraphBeta(对于 beta 版本)。

添加ConfigureServices以下内容:

services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph()
    .AddInMemoryTokenCaches();
Run Code Online (Sandbox Code Playgroud)

.EnableTokenAcquisitionToCallDownstreamApi()之前打电话.AddMicrosoftGraph()

对于 .NET 6,当您创建新的 ASP.NEt Core Web App 项目时,请选择Microsoft identity platform身份验证类型。

在此输入图像描述

然后创建项目,系统会要求您安装所有必需的组件。

在此输入图像描述

在 Program.cs 中添加以下内容:

builder.Services
    .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"))
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph()
    .AddInMemoryTokenCaches();
Run Code Online (Sandbox Code Playgroud)


Mat*_*nny 6

问题是您需要安装以下 NuGet 库的相同版本:

Microsoft.Identity.Web
Microsoft.Identity.Web.MicrosoftGraph
Run Code Online (Sandbox Code Playgroud)

这是必需的,因为两者都使用Microsoft.Identity.Web.TokenAcquisition.

可选:配置 GraphApi

如果您使用Startup类,请添加以下代码:

using Microsoft.Identity.Web;

public void ConfigureServices(IServiceCollection services)
{
  services.AddMicrosoftIdentityWebApiAuthentication(Configuration.GetSection("AzureAd"))
    .EnableTokenAcquisitionToCallDownstreamApi()
    .AddMicrosoftGraph(Configuration.GetSection("GraphApi"))
    .AddInMemoryTokenCaches();
  builder.Services.AddControllersWithViews();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  app.UseRouting();
  app.UseAuthentication();
  app.UseAuthorization();
  app.MapDefaultControllerRoute();
}
Run Code Online (Sandbox Code Playgroud)

如果您使用 C# 脚本启动应用程序,请使用以下代码:

var builder = WebApplication.CreateBuilder(args);
IServiceCollection services = builder.Services;
IConfiguration configuration = builder.Configuration;
/// Code from Startup.ConfigureServices ///

var app = builder.Build();
/// Code from Startup.Configure ///
app.Run();
Run Code Online (Sandbox Code Playgroud)

您的文件中需要以下信息appsettings.json

"AzureAd": {
  "Instance": "https://login.microsoftonline.com",
  "ClientId": "<Guid>",
  "ClientSecret": "<Guid>",
  "TenantId": "<Guid>"
}
"GraphApi": {
  "BaseUrl": "https://graph.microsoft.com/v1.0",
  "Scopes": "user.read"
}
Run Code Online (Sandbox Code Playgroud)

可选:使用 GraphApi

public class HomeController : Controller
{
    private readonly GraphServiceClient _graphClient;

    public HomeController(GraphServiceClient graphClient)
    {
        _graphClient = graphClient;
    }

    [AuthorizeForScopes(Scopes = new string[] { "user.read" })]
    public async Task<string> SayMyName()
    {
        User user = await _graphClient.Me.Request().GetAsync();
        return user.DisplayName;
    }
}
Run Code Online (Sandbox Code Playgroud)

我建议查看:https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/tree/master/2.%20Web%20API%20now%20calls%20Microsoft%20Graph/待办事项列表服务