将 AddMicrosoftIdentityWebApp 与 IdentityServer4 结合使用是否幸运?

Mer*_*ijn 5 azure-active-directory openid-connect asp.net-core identityserver4

我正在尝试将 Microsoft 配置为 Identityserver4 中的外部登录提供程序。我通过使用以下身份服务器的文档成功了AddMicrosoftAccount

services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
 {
  microsoftOptions.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
  microsoftOptions.ClientId = configuration["MicrosoftLoginProvider:ClientId"];
  microsoftOptions.ClientSecret = configuration["MicrosoftLoginProvider:ClientSecret"];
 });
Run Code Online (Sandbox Code Playgroud)

然而,我没有运气让单点退出工作。该文档与 Microsoft 的文档一致,网址为https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/microsoft-logins?view=aspnetcore-5.0

但是,如果按照说明在 Microsoft 开发人员门户 (portal.azure.com) 中创建应用程序,该门户上的示例代码会建议采用不同的方式。门户为我生成的示例应用程序(WebApp-OpenIDConnect-DotNet)正在使用AddMicrosoftIdentityWebApp

 services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
Run Code Online (Sandbox Code Playgroud)

由于该应用程序是开箱即用的,包括单点注销,我想知道这是否是我必须继续的方式。

然而,令我惊讶的是,我找不到任何有关如何在 IdentityServer4 中集成此方法的文档/博客。我自己几乎可以正常工作,但有一些奇怪的问题。

有人可以澄清使用是否AddMicrosoftIdentityWebApp是将 Microsoft 作为外部身份提供商添加到 Identityserver4 的方法吗?有人成功使用AddMicrosoftIdentityWebAppIdentityServer4 吗?

感谢您的帮助!

Mer*_*ijn 4

我想出了如何让它发挥作用。

事实上,我只需要做两件事。

首先,我必须删除对 Microsoft 生成的示例代码的OpenIdConnectDefaults.AuthenticationScheme调用。AddAuthentication所以代码就变成了:

  services.AddAuthentication()
     .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
Run Code Online (Sandbox Code Playgroud)

然后,在从临时 cookie 读取外部身份的代码中,我必须使用 CookieAuthenticationDefaults.AuthenticationScheme。因此,该代码现在如下所示:

var authenticationResult = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
Run Code Online (Sandbox Code Playgroud)

就这些。