为什么在 Asp.net(旧版 .Net Framework)中设置 Azure AD / Identity SSO 时需要客户端密钥,但在 asp.net core 中不需要?

问题描述 投票:0回答:1

按照 Entra 门户 (https://entra.microsoft.com/) 的应用程序注册部分中的“快速入门”指南进行操作时,我注意到了一个关键区别。

当我下载并尝试运行 Asp.net(旧版)的示例项目时,我收到一条错误消息,指出需要客户端密钥。但是,解决方案中包含的文档和自述文件并未提及客户端密钥是必需的步骤。

相比之下,asp.net core 的类似示例项目无需客户端密钥即可运行。

为什么会存在这种差异?我是不是错过了什么?

该应用程序是“机密客户端”的依据是什么?

错误中的帮助链接位于此处 -> https://aka.ms/msal-net-client-credentials

我不明白我的 ASP.NET Core 应用程序如何或为何可以在没有客户端密钥的情况下工作。但对于遗留的 ASP.NET 应用程序来说,这似乎是必需的。如果可能的话,我想一致地配置我的应用程序注册。另外,如果事实证明,对于 ASP.NET Core 应用程序来说,最好的做法是拥有客户端密钥,我会这样做。我的另一个担忧是我不得不担心它们会过期。

注意:我仅使用 Azure 服务在服务器端 Web 应用程序中进行身份验证。我没有使用 graph api 或其他任何东西。

编辑:

我注意到网站上的示例代码与下载的示例项目的一个关键区别:

示例项目 -> Startup.cs

public void Configuration(IAppBuilder app)
{
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();

        app.AddMicrosoftIdentityWebApp(factory);
        factory.Services
            .Configure<ConfidentialClientApplicationOptions>(options => { options.RedirectUri = "https://localhost:44368/"; })
            .AddMicrosoftGraph()
            .AddInMemoryTokenCaches();
        factory.Build();
}

Entra Portal 快速入门指南网站示例 ->startup.cs

public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        // Sets the client ID, authority, and redirect URI as obtained from Web.config
        ClientId = clientId,
        Authority = authority,
        RedirectUri = redirectUri,
        // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it's using the home page
        PostLogoutRedirectUri = redirectUri,
        Scope = OpenIdConnectScope.OpenIdProfile,
        // ResponseType is set to request the code id_token, which contains basic information about the signed-in user
        ResponseType = OpenIdConnectResponseType.CodeIdToken,
        // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
        // To only allow users from a single organization, set ValidateIssuer to true and the 'tenant' setting in Web.> config to the tenant name
        // To allow users from only a list of specific organizations, set ValidateIssuer to true and use the ValidIssuers parameter
        TokenValidationParameters = new TokenValidationParameters()
        {
            ValidateIssuer = false // Simplification (see note below)
        },
        // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to > the OnAuthenticationFailed method
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthenticationFailed = OnAuthenticationFailed
        }
    }
);
}

如果我随后更新代码以遵循快速入门指南上的模式,我可以获得所需的结果,并且不需要客户端密码进行身份验证。

注意:Authority必须是带有租户ID的URL,如:

“https://login.microsoftonline.com/{tenantID}/v2.0”

现在我想知道,为什么下载的示例和快速入门解释之间存在这种差异,以及对于旧版 ASP.NET MVC 应用程序的推荐方法是什么?

此外,使用 Microsoft Graph 的示例代码部分似乎不再适用于这种方法。这对我来说不会是问题,但我也尝试过让 Graph 工作,但我无法弄清楚。

我什至尝试采用混合方法,但这会产生相同的错误,要求机密客户端提供客户端密钥:

OwinTokenAcquirerFactory factory = TokenAcquirerFactory.GetDefaultInstance<OwinTokenAcquirerFactory>();

        app.AddMicrosoftIdentityWebApp(factory);
        factory.Services
            .Configure<OpenIdConnectAuthenticationOptions>(options => {
                // Sets the client ID, authority, and redirect URI as obtained from Web.config
                options.ClientId = "{clientID}";
                options.Authority = "https://login.microsoftonline.com/{tenantID}/v2.0";
                options.RedirectUri = "https://localhost:44368/";
                // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it's using the home page
                options.PostLogoutRedirectUri = "https://localhost:44368/";
                options.Scope = OpenIdConnectScope.OpenIdProfile;
                // ResponseType is set to request the code id_token, which contains basic information about the signed-in user
                options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
                // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
                // To only allow users from a single organization, set ValidateIssuer to true and the 'tenant' setting in Web.> config to the tenant name
                // To allow users from only a list of specific organizations, set ValidateIssuer to true and use the ValidIssuers parameter
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false // Simplification (see note below)
                };
                // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to > the OnAuthenticationFailed method
                options.Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed
                };
            })
            .AddMicrosoftGraph()
            .AddInMemoryTokenCaches();
        factory.Build();
asp.net-mvc azure azure-active-directory single-sign-on azure-ad-msal
1个回答
0
投票

这两个代码(旧版 ASP.NET)对于验证应用程序都是正确的,都使用不同的协议和中间件。

  • 从 Entra ID 下载的代码还包含
    MicrosoftGraph
    in-memory token caching
    的配置。
.AddMicrosoftGraph()
.AddInMemoryTokenCaches();
  • 由于它是使用
    ConfidentialClientApplicationOptions
    配置的,所以这里必须有Client Secret。您可以通过在ConfidentialClientApplicationOptions上右键=>
    Go to Definition
    看到下面的代码。

enter image description here

  public class ConfidentialClientApplicationOptions : ApplicationOptions
  {     
      public string ClientSecret { get; set; }
  }

所有机密客户端都可以选择使用客户端机密或证书凭证。

  • 另请参阅SOThread,它解释了在没有客户端密钥的情况下使用的选项。

  • .NET Core App 也配置了

    OpenIdConnect
    ,这里也可以使用 Client Secret。

我的另一个担忧是我不得不担心它们会过期。

对于旧版 ASP.NET MVC 应用程序,推荐使用哪种方法?

  • 最好使用 OIDC 流程,它允许在没有客户端机密的情况下工作,避免它们过期和暴露的风险。此外,该流程依赖于浏览器来处理身份验证。
© www.soinside.com 2019 - 2024. All rights reserved.