如何解决Azure 的 ASP.Net 5 身份验证 - 声明似乎已更改 简要更详细
简要
在使用 Azure ADFS 进行身份验证时是否应该使用 OpenIdConnectDefaults.AuthenticationScheme
?
更详细
我有一个最近从 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 Web 页面以获取更多信息....似乎要搜索很多东西才能找到我想要的东西。
我确实读到 Visual Studio Preview 版本有一个更新的项目模板,所以我创建了一个新项目并将其连接到 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");
所以,我可以用它运行,它会工作......
但是,我想知道使用 OpenIdConnectDefaults.AuthenticationScheme
是否是最新 Microsoft Identity 和 Azure ADFS 的首选选项?我不得不使用魔法字符串“preferred_username”而不是 ClaimTypes.Upn
的事实让我有些怀疑。
有人对此有深入的了解吗?
解决方法
我不得不使用魔法字符串“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/ 写了一篇关于此的博客。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。