【问题标题】:Any luck in using AddMicrosoftIdentityWebApp in combination with IdentityServer4?将 AddMicrosoftIdentityWebApp 与 IdentityServer4 结合使用有什么运气吗?
【发布时间】:2021-02-25 09:43:29
【问题描述】:

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

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

但是,我没有让单点注销工作的运气。该文档与 Microsoft 在 https://docs.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"));

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

然而,令我惊讶的是,我找不到任何关于如何将这种方法集成到 IdentityServer4 中的文档/博客。我几乎可以自己完成它,但有一些奇怪的问题。

有人可以澄清使用AddMicrosoftIdentityWebApp 是否是将Microsoft 作为外部身份提供者添加到Identityserver4 的方法? 有人成功让AddMicrosoftIdentityWebApp 与 IdentityServer4 一起工作吗?

感谢您的帮助!

【问题讨论】:

    标签: asp.net-core azure-active-directory identityserver4 openid-connect


    【解决方案1】:

    翻遍后发现了这个说法here

    Microsoft.Identity.Web 是在 ASP.NET Core Web 应用程序和 Web API 中使用 Azure AD 的更简单方法。

    它不会以任何方式替换 ASP.NET Identity,它不会替换 AddJwtBearerAddCookie 或任何较低的级别原语,但它确实为 Azure AD 正确使用和配置它们。

    它不适用于非 Azure 身份提供程序。它取代了 .NET 5.0 中已过时的 AzureAD.UI 和 AzureADB2C.UI

    因此,结论是 Microsoft.Identity.Web 不能在 Azure AD 之外工作,因此不能与 IdentityServer 一起使用。

    如果你确实让它工作,请告诉我!

    【讨论】:

    • 谢谢@Tore。但是,我想知道您的结论是否正确。来自声明“它不适用于非 Azure 身份提供程序”。在您提到的页面上,我了解您不能将包用于例如谷歌。这并不意味着它不能用于将 Azure AD 配置为 IdentityServer4 中的外部身份提供者,不是吗?
    • 怀疑它会影响/帮助 IdentityServer Azure AD 通信,因为与它在客户端应用程序中解决的逻辑相比,这种通信非常成熟且“简单”。
    • 实际上,只需进行一些更改,它就可以像魅力一样工作(请参阅下面的答案)。如我所愿,现在即使注销也终于可以使用了。再次感谢您的帮助。
    • 很好奇,您的实际效果如何?如果你能在这里发布它作为答案会很好:)
    • 我已经添加了解决方案作为我的问题的答案。详情见下文。
    【解决方案2】:

    我想出了如何让它工作。

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

    首先,我必须在 Microsoft 生成的示例代码中删除对 AddAuthentication 的调用中的 OpenIdConnectDefaults.AuthenticationScheme。于是代码变成了:

      services.AddAuthentication()
         .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
    

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

    var authenticationResult = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    

    仅此而已。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      相关资源
      最近更新 更多