You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET MVC应用中配置带AcquireTokenInteractive的GraphServiceClient

如何在ASP.NET MVC中配置AcquireTokenInteractive解决MsalUiRequiredException?

我按照官方示例在ASP.NET MVC应用中使用GraphServiceClient。之前的问题解决后,现在需要添加AcquireTokenInteractive来修复以下错误(此为建议方案):

MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call

现有配置

依赖注入配置

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
        .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
        .AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))                 
        .AddInMemoryTokenCaches();

HomeController测试代码

private readonly GraphServiceClient _graphServiceClient;
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger, GraphServiceClient graphServiceClient, IConfiguration configuration)
{
    _logger = logger;

    _graphServiceClient = graphServiceClient;
    usrAsync();
}

async Task usrAsync()
{
    var user = await _graphServiceClient.Me.GetAsync();
    //上述代码抛出MsalUiRequiredException异常
}

使用的包版本

<PackageReference Include="Microsoft.Identity.Web" Version="2.15.2" />
<PackageReference Include="Microsoft.Identity.Web.DownstreamApi" Version="2.15.2" />
<PackageReference Include="Microsoft.Identity.Web.GraphServiceClient" Version="2.15.2" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="2.15.2" />

请问如何配置AcquireTokenInteractive?


内容的提问来源于stack exchange,提问作者Mister 832

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 08:46:11