app.UseWindowsAzureActiveDirectoryBearerAuthentication 从 ADAL 升级到 MSAL 后停止工作

app.UseWindowsAzureActiveDirectoryBearerAuthentication stopped working after upgrade from ADAL to MSAL

我正在 运行 使用 MSAL 进行一些测试,但不幸的是它没有按预期工作。

我已经为 ASP.NET MVC (.net 4.6) + Angular 1.6 SPA 应用程序配置了 ADAL 和 ADAL Angular。一切正常,但后来我决定尝试 MSAL.

我在 Startup.Auth.cs 中配置的提供商 OnValidateIdentity 处理程序被 ADAL 正确命中,我可以添加其他声明:

Provider = new OAuthBearerAuthenticationProvider
                    {
                        OnValidateIdentity = async context =>
                        {

现在我为 Angular JS 更改为 MSAL,我得到 ID TokenAccess Token 但我的 OnValidateIdentity 处理程序不是又被打了

使用 MSAL 时使用 app.UseWindowsAzureActiveDirectoryBearerAuthentication 是否仍然有效?

app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                {
.
.
.

MSAL 用于 converged/v2.0 应用程序注册,而 ADAL 通常用于 v1.0 应用程序注册,

如果您尝试迁移到 v2 端点,则应使用 portal.azure.com 中的新门户创建新应用程序。除此之外,这些文档还介绍了如何创建 v2.0 应用程序注册:https://docs.microsoft.com/en-us/graph/auth-register-app-v2

有关从 v1 迁移到 v2 端点的更多信息,请参阅此资源。 https://azure.microsoft.com/en-gb/resources/samples/active-directory-dotnet-v1-to-v2/

关于使用 app.UseWindowsAzureActiveDirectoryBearerAuthentication

的细节

// NOTE: The usual WindowsAzureActiveDirectoryBearerAuthenticaitonMiddleware uses a // metadata endpoint which is not supported by the v2.0 endpoint. Instead, this // OpenIdConenctCachingSecurityTokenProvider can be used to fetch & use the OpenIdConnect // metadata document.

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
{
    AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider("https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration")),
});

这是从 startup.cs 中引用的:https://github.com/AzureADQuickStarts/AppModelv2-NativeClient-DotNet/blob/a69a4cb41e821f0ea8dddc937ea401a03e2f49fe/TodoListService/App_Start/Startup.Auth.cs

可以在此处找到一些比较 v1/v2 示例应用程序的好读物:https://simonlamb.codes/2017/02/27/net332-introduction-to-authentication-on-azure-active-directory/