【问题标题】:Identityserver 4 and Azure AD身份服务器 4 和 Azure AD
【发布时间】:2023-03-14 07:45:01
【问题描述】:

我正在研究在基于 C# 的 MVC 应用程序中使用 Identity Server 4 进行身份验证。我想使用存储在 Azure AD 中的帐户作为有效用户的来源,但文档似乎只提到了 Google 和 OpenID,并且只是顺便提到了 Azure。

是否有人知道有关如何在将 Azure AD 与 Identity Server 4 一起使用的上下文中使用它的任何好的文档和/或教程?

【问题讨论】:

    标签: c# azure-active-directory identityserver4


    【解决方案1】:

    您可以从 IdentityServer 使用登录到 Azure AD,就像您从例如使用登录到 IdentityServer 一样。 Javascript 或 MVC 应用程序。

    我最近已经这样做了,您只需将 OpenIdConnect 选项注册到 Azure Ad,如下所示:

    public void ConfigureAuth(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
    
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
            });
    }
    

    在此处了解更多信息:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapp-dotnet

    然后您应该在登录操作中调用 ChallengeAsync 方法:

    var authenticationProperties = new AuthenticationProperties { RedirectUri = "your redirect uri" };
    await HttpContext.Authentication.ChallengeAsync(your policy, authenticationProperties);
    

    然后提供一个回调方法作为 GET 方法,然后按照 IdentityServer 示例中提供的外部登录示例:https://github.com/IdentityServer/IdentityServer4.Samples/blob/dev/Quickstarts/4_ImplicitFlowAuthenticationWithExternal/src/QuickstartIdentityServer/Quickstart/Account/AccountController.cs

    【讨论】:

    • 感谢您提供的信息。我刚从 IS4 开始,所以我需要问你一些事情:我阅读了有关外部登录的文档,示例显示了一个按钮,我想它会将你重定向到 google 身份验证页面,因此我还假设你的示例重定向到微软登录页面,还是我错了?如果是真的,是否可以自动验证知道其凭据的用户?我的意思是,有人将用户的凭据(微软)发送到我的 JS 应用程序,然后我将它们发送到 IS4 并尝试获取令牌
    • 这就是 OpenID Connect 的真正意义所在;您将处理密码的麻烦委托给另一个系统,在本例中为 Microsoft。您必须添加诸如 login.microsoft.com 之类的权限或类似权限,然后在重定向时将使用此权限。 OpenID Connect 不是 Microsoft 特定的;它只是对如何使用 3rd 方提供商进行登录的规范。
    【解决方案2】:

    有一个sample with Azure AD on github,来自IdentityServer samples 中提供的外部登录示例。

    该示例还修复了一个已知问题"State parameter generated by middleware is too large for Azure AD #978"

    【讨论】:

    • github.com/aspnet/Security/issues/1310 上面给出的代码示例现在已经过时,来自 Haok 的 cmets “旧的 1.0 身份验证堆栈不再起作用,并且在 2.0 中已过时”。这特别涉及上述存储库设置 IdentityServer startup.cs 的 Config / cookie 身份验证的方式。据我所知,迁移是在 6 月 17 日左右完成的。
    • 如果你把app.UseCookieAuthentication放进去,然后看看构造函数上的cmets,它会带你到上面的链接。我仍然赞成你的帮助,如果我能从那个仓库中插入我需要的代码,那就太棒了。
    • @JakeJ,您可以在 GitHub 存储库上创建问题,要求升级到 Core 2.0 甚至提交 Pull Request。
    【解决方案3】:

    IdentityServer4 有“使用外部身份提供者登录”的文档

    http://docs.identityserver.io/en/latest/topics/signin_external_providers.html#state-url-length-and-isecuredataformat

    不幸的是,它不完整,但这就是我所做的:

    Startup.cs 用于 .NET 5,Program.cs 用于 .NET 6:

    services.AddAuthentication()
          .AddOpenIdConnect("aad", "Azure AD", options =>
                {
                    options.ClientSecret = "<ClientSecret>";
                    options.ResponseType = OpenIdConnectResponseType.Code;
                    options.ClientId ="<ClientId>";
                    options.Authority = "https://login.microsoftonline.com/<TenantId>/";
                    options.CallbackPath = "/signin-oidc";
                })
            .AddIdentityServerJwt();
    

    然后您将在“使用其他服务登录”下看到一个外部登录。

    完成登录后,您应该会看到此消息。

    点击Register后默认设置卡住了。这是由于新电子邮件未得到确认。这可以通过设置SignIn.RequireConfirmedAccount = false来解决

    services.AddDefaultIdentity<ApplicationUser>(options => 
        options.SignIn.RequireConfirmedAccount = true)
    

    对于新用户,您还可以使用“重新发送电子邮件确认”或在 [dbo].[AspNetUsers] 中将 EmailConfirmed 设置为 true。

    Azure AD 设置。您还需要在Certificates &amp; secrets 下添加一个客户端密码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-17
      • 2022-11-03
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      相关资源
      最近更新 更多