当 AzureAD 不是默认身份验证方案时,GraphServiceClient 会抛出异常

问题描述 投票:0回答:1

在我的 ASP.NET Core 应用程序中,我有多种身份验证方案。

如果

GraphServiceClient

 不是默认方案,则 
IDW10503: Cannot determine the cloud Instance. The provided authentication scheme was ''
抛出
OpenIdConnect
异常。我需要“cookie”作为默认方案。

我的配置:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddNegotiate()
    .AddMicrosoftIdentityWebApp(builder.Configuration, configSectionName: "Authentication:AzureAD");

builder.Services.AddRazorPages()
    .AddMicrosoftIdentityUI();

我的页面:

[Authorize(AuthenticationSchemes = OpenIdConnectDefaults.AuthenticationScheme)]
public class TestModel : PageModel
{
    public int UnreadMessages { get; set; }

    public async Task<IActionResult> OnGetAsync([FromServices] GraphServiceClient graph)
    {
        UnreadMessages = (await graph.Me.MailFolders["Inbox"].Messages
                                     .Request()
                                     .Filter("isRead ne true")
                                     .GetAsync()).Count;
        return Page();
    }
}

我可以配置 GraphServiceClient 使其工作吗? 谢谢!

asp.net-core microsoft-graph-api asp.net-identity
1个回答
0
投票

我找到了一种方法来告诉 GraphServiceClient 使用什么身份验证模式使用

WithAuthenticationScheme
方法:

var messages = await graph.Me.MailFolders["Inbox"].Messages
    .Request()
    .WithAuthenticationScheme(OpenIdConnectDefaults.AuthenticationScheme)
    .Filter("isRead ne true")
    .GetAsync();
© www.soinside.com 2019 - 2024. All rights reserved.