【发布时间】:2021-10-04 00:32:51
【问题描述】:
我在 .NET 5 中创建了一个 Web api,然后将其发布到 Azure。
这是我在启动时如何配置 web api:
public void ConfigureServices(IServiceCollection services)
{
services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd");
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints
.MapControllers()
.RequireAuthorization();
});
}
这就是 appsettings.json:
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "***.com", //Domain name configured in Azure
"TenantId": "***", // Tenant Id configured in Azure
"ClientId": "***", // Client Id configured in Azure
"CallbackPath": "/signin-oidc"
}
现在我启用了 Azure AD 身份验证。步骤如下:
当我点击 Get New Access Token 时,我得到了正确的结果:
但是现在,如果我调用我的 API,我会收到 401 Unauthorized 错误:
我的配置还有什么问题?有什么帮助吗?
谢谢
【问题讨论】:
标签: azure asp.net-web-api azure-active-directory .net-5