【问题标题】:Redirect URI with Client Credential Flow使用客户端凭据流重定向 URI
【发布时间】:2019-02-21 20:12:26
【问题描述】:

我正在研究使用 MSAL 和客户端凭据流,但是,我不完全了解一件事。

在微软提供的示例中: https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2/blob/master/daemon-console/Program.cs

以下代码用于获取访问令牌:

var clientCredentials = new ClientCredential(_clientSecret);
var app = new ConfidentialClientApplication(_clientId, _authority, "https://daemon", clientCredentials, null, new TokenCache());
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
AuthenticationResult result = await app.AcquireTokenForClientAsync(scopes);

在这种情况下,redirectUri 是怎么回事?

我尝试了不同的值作为redirectUri,它似乎以任何一种方式工作......但如果我添加相对路径或null,则无法获得令牌。这个值应该是多少?

对于控制台应用程序,侦听 URL 没有什么意义,但是,ConfidentialClientApplication 的文档说它是必需的。

【问题讨论】:

  • RedirectURI 应该为 STS(在本例中为 AzureAD)可以使用令牌回调的 Web 应用程序提供 URL。您正确地怀疑此值对于客户端凭据授予无关紧要。我“猜测”(但不确定)重定向 URI 的概念源于应用程序类型是 Web 应用程序。Azure AD 只有 Web 应用程序或本机应用程序类型。对于控制台应用程序/守护程序 - 它可以充当机密客户端并使用客户端机密您将选择 Web 应用程序类型(即使它是反直觉的,并且此示例的文档和其他地方会提到相同的内容)
  • 这对我来说似乎很奇怪,因为代表似乎也不需要redirectUri,如果是这种情况,那么我在ConfidentialClientApplication 构造函数中看不到redirectUri 的单一用途。此外,当向此 redirectUri 添加某些值时,acquireToken 将失败,因此它会触发某些事情。

标签: asp.net-core oauth-2.0 .net-core azure-active-directory msal


【解决方案1】:

要使用客户端凭据流请求访问令牌,应用程序将使用应用程序的凭据向 Azure AD 的令牌端点发送 HTTP POST 令牌请求,AAD 将返回访问令牌作为响应,在这种情况下不需要重定向 url。根据源代码,也没有使用重定向网址:

private async Task<AuthenticationResult> AcquireTokenForClientCommonAsync(IEnumerable<string> scopes, bool forceRefresh, ApiEvent.ApiIds apiId, bool sendCertificate)
{
    Authority authority = Instance.Authority.CreateAuthority(ServiceBundle, Authority, ValidateAuthority);
    AuthenticationRequestParameters parameters = CreateRequestParameters(authority, scopes, null,
        AppTokenCache);
    parameters.IsClientCredentialRequest = true;
    parameters.SendCertificate = sendCertificate;
    var handler = new ClientCredentialRequest(
        ServiceBundle,
        parameters,
        apiId,
        forceRefresh);

    return await handler.RunAsync(CancellationToken.None).ConfigureAwait(false);
}

但是此时你应该在初始化ConfidentialClientApplication时提供一个有效的url。

【讨论】:

  • Tl:DR;构造函数是愚蠢的,当它不需要带有客户端凭据的 URL 时,它需要一个 URL。
  • 有趣的是,如果您确实提供了相对 url(例如“/signin-oidc”)或 null,它将无法 AcquireToken,因此它用于某些东西......
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2012-12-17
  • 2019-04-28
  • 1970-01-01
相关资源
最近更新 更多