首页 > 解决方案 > 用于 Azure AD 令牌验证的 ASP.NET Core 自定义身份验证筛选器

问题描述

我目前正在开发一个需要使用 Azure AD 令牌的项目,该令牌运行良好。我在 Azure 门户中注册我的应用程序并在我的项目中使用客户端 ID,这与此代码(在 中startup.cs)一起工作正常:

services.AddMicrosoftIdentityWebApiAuthentication(configuration, "AzureAd");

这些设置在appsettings.json

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "TenantId": "35234523452345",
    "ClientId": "25234523452345",
    "Audience": "api://25234523452345"
},

我只想标记一个 API 调用,但使用上面的代码,它将适用于所有 API。我可以使用将使用 Azure 令牌进行身份验证的自定义筛选器吗?

有人可以帮我提供代码示例吗?

标签: c#asp.net-coreazure-active-directoryaccess-token

解决方案


An ASP.NET Core web application that authenticates Azure AD users and calls a web API using OAuth 2.0 access tokens.

Here you can find the ASP.NET Core samples about JwtBearer. Here the Azure AD samples demonstrate WebApp calling WebApi (also bearer for the WebAPI, OIDC for the App FrontEnd.

Sample Code: https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore

Reference SO Thread: Azure AD Authentication in ASP.NET Core 2.2


推荐阅读