首页 > 解决方案 > 使用 Azure 进行 ASP.Net 5 身份验证 - 声明似乎已更改

问题描述

简要地

OpenIdConnectDefaults.AuthenticationScheme使用Azure ADFS 进行身份验证时应该使用吗?

更详细

我有一个最近从 3.1 升级到 .NET 5 的 ASP.NET Core 应用程序。

以前,它一直在使用以下 NuGet 包:

<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.9" />

以及我的以下内容StartUp.cs

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => this.Configuration.Bind("AzureAd", options));

今天,我更新了 NuGet 包:

<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="5.0.3" />

并立即收到警告说我正在使用已弃用/过时的代码。

我被引导到Microsoft Identity网页以获取更多信息……似乎需要大量搜索才能找到我想要的东西。

虽然我确实读到 Visual Studio 预览版有一个更新的项目模板,所以我创建了一个新项目并将其连接到 Azure,并且我使用我的域凭据登录。极好的!

它使用的相关 NuGet 包似乎是:

<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.3" NoWarn="NU1605" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.3" NoWarn="NU1605" /
<PackageReference Include="Microsoft.Identity.Web" Version="1.1.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.1.0" />

所以,认证完成。现在进入授权......

所以我们有自己的本土授权服务。我们将用户的身份(来自 ADFS)发送到此,它返回他们被允许做的事情。这就是事情破裂的地方......

我们的原始代码使用了 Azure ADFS 响应中的“Upn”声明:

Claim? upnClaim = identity.FindFirst(ClaimTypes.Upn);

这将返回带有电子邮件地址的声明。

但是,这现在返回 null。

以下代码确实使用电子邮件地址获得了声明:

Claim? upnClaim = identity.FindFirst("preferred_username");

所以,我可以用这个运行,它会工作......

但是,我想知道 usingOpenIdConnectDefaults.AuthenticationScheme是否是最新 Microsoft Identity 和 Azure ADFS 的首选选项?我不得不使用魔术字符串“preferred_username”而不是ClaimTypes.Upn让我有些怀疑。

有没有人对此有深入的了解?

标签: azure-active-directoryadfsasp.net-authenticationmicrosoft-identity-web

解决方案


我不得不使用魔术字符串“preferred_username”而不是 ClaimTypes.Upn 的事实让我有些怀疑。

preferred_username不是魔术字符串,它被记录为 AAD 添加到 id 令牌有效负载的声明之一,请参阅https://docs.microsoft.com/azure/active-directory/develop/id-tokens#payload-claims

ASP.NET Core OpenID Connect 提供程序使用的底层库用于映射声明以匹配 .NET 世界中的知名声明。也许 Microsoft.Identity.Web 禁用了该特定行为。

不久前,我在https://mderriey.com/2019/06/23/where-are-my-jwt-claims/上写了一篇博客。


推荐阅读