我正在研究使用MSAL和客户端凭据流,但有一点我不完全理解。
在Microsoft提供的示例中: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的文档说它是必需的。
要使用客户端凭据流请求访问令牌,应用程序将使用应用程序的凭据向Azure AD的令牌端点发送HTTP POST令牌请求,AAD将返回访问令牌作为响应,在此方案中不需要重定向URL。根据源代码,重定向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。