【问题标题】:Microsoft Identity Web invalid token in Blazor WASMBlazor WASM 中的 Microsoft Identity Web 无效令牌
【发布时间】:2020-06-13 14:30:19
【问题描述】:

我刚刚完成了这个尝试:https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore-v2/tree/master/1.%20Desktop%20app%20calls%20Web%20API

我使用的是 Blazor WASM 客户端,而不是 WPF 应用程序。

我将令牌添加到 .net 核心 API 的传出请求中,但我总是得到 401。

Blazor 验证良好并取回令牌,似乎工作正常,但是:

  • 观众的 GUID 有误
  • “scp”(范围)丢失,因此令牌无法使用

如果我从上述链接运行示例并解码令牌,我可以在令牌中看到正确的 AUD 和 SCP。所以这可能与我在 Blazor 中的配置有关?

在 Blazor 中配置

// AD authentication
builder.Services.AddMsalAuthentication(options =>
{
    builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
    options.ProviderOptions.DefaultAccessTokenScopes.Add("api://xxx-xxxx-xxxx-xxxx/access_as_user");
});

builder.Services
       .AddHttpClient<IApiClient, ApiClient>(client => client.BaseAddress = _baseUri)
       .AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

回应

Bearer error="invalid_token", error_description="The audience '63ee4227-xxxx-xxxx-xxxx' is invalid"

观众 GUID 是我的 Blazor 应用注册的客户端 ID

Startup.cs 中的代码

...
services.AddProtectedWebApi(Configuration)
        .AddInMemoryTokenCaches();
...

...
app.UseAuthentication(); 
app.UseAuthorization();
...

知道可能出了什么问题吗?

【问题讨论】:

    标签: .net-core azure-active-directory blazor microsoft-identity-platform


    【解决方案1】:

    当我学习使用 Blazor WebAssembly 时,我关注了 this tutorial,它做了非常相似的事情,并且您可能更容易实现,因为您不必尝试从 WPF 转换任何代码。

    但是,要尝试的一件事是改变这一点:

    options.ProviderOptions.DefaultAccessTokenScopes.Add("api://xxx-xxxx-xxxx-xxxx/access_as_user");
    

    到这里:

    options.ProviderOptions.DefaultAccessTokenScopes.Add("xxx-xxxx-xxxx-xxxx/access_as_user");
    

    在我的应用程序中,我调用了两个 API 端点,一个托管在与 Blazor 应用程序相同的域中,另一个位于单独的域中。为外部域请求令牌时,必须使用 api:// 前缀。但是,对于同一个域,它必须被排除。

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 2020-09-01
      • 2014-07-12
      • 1970-01-01
      • 2020-09-07
      • 2021-02-05
      • 2015-05-07
      • 2019-01-12
      • 2021-10-03
      相关资源
      最近更新 更多