【问题标题】:How can I use authentication handlers for both Microsoft Account and ADFS in the same application?如何在同一应用程序中同时为 Microsoft 帐户和 ADFS 使用身份验证处理程序?
【发布时间】:2021-11-19 21:10:52
【问题描述】:

我们有一个已安装在多个客户端上的 .NET Core Web 应用程序。我们使用 appsettings 文件来配置客户端之间的细微差别,例如数据库连接。我们一直在为每个客户使用 Microsoft 帐户身份验证。

现在我们有一个希望我们使用 ADFS 身份验证的客户端。理想情况下,我们希望能够使用我们的 appsettings 文件进行配置,但我不确定如何执行此操作。那么,我们如何同时使用 Microsoft 帐户和 ADFS 身份验证,并指定使用哪个?下面是我的启动文件的身份验证部分。出于保密原因,我省略了一些内容。我不知道我是否需要所有这些:

services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
            {
                microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ApplicationId"];
                microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:Password"];
                microsoftOptions.AuthorizationEndpoint = Configuration["Authentication:Microsoft:OAuth"];
                microsoftOptions.TokenEndpoint = Configuration["Authentication:Microsoft:Token"];
                microsoftOptions.CallbackPath = new PathString("/auth/callback");
                microsoftOptions.UsePkce = false;
            }).AddWsFederation(options =>
            {
                // MetadataAddress represents the Active Directory instance used to authenticate users.
                options.MetadataAddress = "Omitted";

                // Wtrealm is the app's identifier in the Active Directory instance.
                // For ADFS, use the relying party's identifier, its WS-Federation Passive protocol URL:
                options.Wtrealm = "Omitted";

                // For AAD, use the Application ID URI from the app registration's Overview blade:
                //options.Wtrealm = "api://bbd35166-7c13-49f3-8041-9551f2847b69";
            });

【问题讨论】:

    标签: .net asp.net-core authentication adfs microsoft-account


    【解决方案1】:

    如果我的问题是正确的,我们的目标是如何在 .net Web 应用程序中使用多重身份验证。

    要使其工作,了解身份验证步骤的输出是构建HttpContextUser 属性很重要(在处理各种验证步骤之后,当然)。

    因此,默认情况下,多个身份验证过程不会同时进行。我们可以制作一个自定义的身份验证处理程序以使其成为可能,但这会使身份验证成分紧密耦合在一起。

    例如,如果我们要创建一个MsAuth+WsFedAuthenticationHandler,它将以MsAuthWsFed 作为原材料,那么如果客户之后需要谷歌身份验证。这不是开箱即用的,而是对新的MsAuth+WsFed+GoogleAuthAuthenticationHandler 进行修改。这真的很麻烦。

    相反,我们应该采取行动,创建一个自定义PolicyScheme,它负责选择哪个AuthenticationScheme 将解析请求身份验证。例如:

    services
        .AddAuthentication(opts => opts.DefaultScheme = "MyElectedAuthenticationScheme")
        .AddMicrosoftAccount()
        .AddWsFederation()
        .AddPolicyScheme("MyElectedAuthenticationScheme", "My custom authentication electing logic process"
            , opts => {
                // The purpose of this was just some logic to choose the right authentication scheme to handle out request.
                opts.ForwardDefaultSelector = ctx => ctx.Request.Headers.ContainsKey("some header that only Microsoft Accout validating have") ? "Microsoft Account Authentication scheme goes here" : "Ws Federation Authentication scheme goes here";
            });
    

    然后,通过这种方法,可以根据需要使用尽可能多的身份验证提供程序,只需注册它们并构建一个合理的选举逻辑来为每个请求选择正确的身份验证方案。那么我们会很高兴很长一段时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 2017-02-05
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多