如何解决ASP.NET Core Microsoft Identity 注销“方案字段是必需的”
我将 ASP.NET Core 5 与 Azure AD 身份验证结合使用。
我创建了一个新的模板 Web 应用,使用 Azure AD 登录和注销效果很好。
但是,当我将 Startup.cs
和 appsettings.json
中的相关代码从模板 Web 应用复制到另一个 Web 应用时,当我单击 /MicrosoftIdentity 的注销链接时,我收到了 400 错误响应/账户/退出
["The scheme field is required."]
在 Startup.cs
中,我添加了:
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
和
services.AddRazorPages()
.AddMicrosoftIdentityUI();
我已经比较了模板 Web 应用程序中的请求和另一个无效的 Web 应用程序中的请求,我看不到 /MicrosoftIdentity/Account/SignOut 的请求标头有任何区别。
我错过了什么“计划”?
解决方法
我查看了 Microsoft.Identity.Web
(see here) 和 MVC 的源代码,我认为唯一合理的解释是您在第二个项目中启用了 nullable reference types C# 8 功能.
当我启用可空值时,我能够重现该错误。
发生的事情是 SignOut
操作具有以下属性和签名:
// From Microsoft.Identity.Web.UI/AccountController.cs
// See: https://github.com/AzureAD/microsoft-identity-web/blob/master/src/Microsoft.Identity.Web.UI/Areas/MicrosoftIdentity/Controllers/AccountController.cs
/// <summary>
/// Handles the user sign-out.
/// </summary>
/// <param name="scheme">Authentication scheme.</param>
/// <returns>Sign out result.</returns>
[HttpGet("{scheme?}")]
public IActionResult SignOut([FromRoute] string scheme)
{
...
}
当您启用可空值时,这会产生您观察到的错误,因为方法签名中的 scheme
不再是可选的。该问题类似于this GitHub issue。
我怀疑,虽然 ASP.NET Core 和(我想)在 2020 年向其大部分组件添加了可空支持,但 Microsoft.Identity.Web
没有得到相同的处理(它实际上与 ASP.NET Core 完全分开) ; 它是 AzureAD 的一部分。
也许更有趣的是,为什么其他帐户操作(具有类似签名)不会导致相同的错误。我既不使用可空值,也不使用 Microsoft.Identity.Web,因此我没有与此相关的信息。
当然,如果我错了,请告诉我,并且您没有启用可空值。 :)
,在@Leaky 关于查看 Microsoft.Identity.Web
SignOut()
source code 的指针之后,我更改了注销 URL 结构以显式包含 scheme
参数,如下所示:
@if (User.Identity.IsAuthenticated)
{
var parms = new Dictionary<string,string>
{
{ "scheme",OpenIdConnectDefaults.AuthenticationScheme }
};
<a asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut"
asp-all-route-data="parms">
Sign out
</a>
}
现在当我将鼠标悬停在链接上时,而不是:
https://localhost:44350/MicrosoftIdentity/Account/SignOut
退出链接如下所示:
https://localhost:44350/MicrosoftIdentity/Account/SignOut/OpenIdConnect
并且完全退出可以正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。