【发布时间】:2021-04-08 11:40:19
【问题描述】:
我有一个从 https://github.com/Azure-Samples/ms-identity-blazor-wasm/tree/main/WebApp-graph-user/Call-MSGraph 创建的 blazor webassembly 项目。
它基本上是在您将 .net 核心模板用于通过 AD B2B 使用身份验证的 Blazor 应用程序时创建的项目
dotnet new blazorwasm -au SingleOrg --client-id "{CLIENT ID}" -o {APP NAME} --tenant-id "{TENANT ID}"
然后,当用户登录时,我能够调用 graph.api。然后,我尝试使用 https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-3.1 中所述的身份验证调用我自己的 Api。
我用过
builder.Services.AddHttpClient<ITestDataService, TestDataService>(
client => client.BaseAddress = new Uri("https://localhost:44342/"))
.AddHttpMessageHandler(x =>
{
var handler = x.GetRequiredService<AuthorizationMessageHandler>()
.ConfigureHandler(new[] { "https://localhost:44342/" },
scopes: new[] { "https://graph.microsoft.com/User.Read" });
return handler;
});
我可以看到调用 Api 时附加了一个令牌,但身份验证失败 (401)。该 api 是从 B2B AD 的 Visual Studio 模板生成的,并使用也用于 Blazor 应用程序的配置。
这是它的 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureAd", options));
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors(policy =>
policy.WithOrigins("http://localhost:5000",
"https://localhost:5001")
.AllowAnyMethod()
.WithHeaders(HeaderNames.ContentType,
HeaderNames.Authorization,
"x-custom-header")
.AllowCredentials());
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
你知道缺少什么吗?
完整的源代码在 github https://github.com/mathiasfritsch/blazor-calls-api
【问题讨论】:
标签: azure-active-directory blazor-webassembly