Asp.net core 使用客户端凭据流重定向URI
我正在研究使用MSAL和客户端凭据流,但是,有一件事我还不完全了解 在Microsoft提供的示例中: 以下代码用于获取访问令牌:Asp.net core 使用客户端凭据流重定向URI,asp.net-core,oauth-2.0,.net-core,azure-active-directory,msal,Asp.net Core,Oauth 2.0,.net Core,Azure Active Directory,Msal,我正在研究使用MSAL和客户端凭据流,但是,有一件事我还不完全了解 在Microsoft提供的示例中: 以下代码用于获取访问令牌: var clientCredentials = new ClientCredential(_clientSecret); var app = new ConfidentialClientApplication(_clientId, _authority, "https://daemon", clientCredentials, null, new TokenCac
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);
在这种情况下,重定向URI是什么
我尝试了不同的值作为重定向URI,它似乎以任何一种方式工作。。。但如果我添加相对路径或null,它将无法获取令牌。这个值应该是多少
对于控制台应用程序来说,监听URL没有什么意义,但是,机密客户端应用程序的文档说明它是必需的。若要使用客户端凭据流请求访问令牌,应用程序将使用应用程序的凭据向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);
}
专用异步任务AcquireTokenForClientCommonAsync(IEnumerable作用域、bool forceRefresh、ApiEvent.apids apid、bool sendCertificate)
{
Authority=Instance.Authority.CreateAuthority(ServiceBundle、Authority、ValidateAuthority);
AuthenticationRequestParameters=CreateRequestParameters(权限、范围、空、,
AppTokenCache);
parameters.IsClientCredentialRequest=true;
parameters.SendCertificate=SendCertificate;
var handler=new ClientCredentialRequest(
ServiceBundle,
参数,
蜂鸟,
强制刷新);
返回await handler.RunAsync(CancellationToken.None).ConfigureAwait(false);
}
但是在此时初始化
机密客户端应用程序时,您应该提供一个有效的url。重定向URI应该为web应用程序提供一个url,其中STS(在本例中为AzureAD)可以使用令牌回调。。由于您正确地怀疑此值对客户端凭据授予不重要。。我“猜测”(但不确定)重定向URI的这个概念源于web应用程序的应用程序类型。。Azure AD只有web应用或本机应用类型。。对于控制台应用程序/守护程序-它可以充当机密客户端并使用客户端机密,您将选择web应用程序类型(尽管这与直觉相反,本示例和其他地方的文档也会提到这一点)这对我来说似乎很奇怪,因为代表似乎也不需要重定向URI,如果是这种情况,那么我看不到在机密ClientApplication构造函数中使用重定向URI。此外,当向该重定向URI添加某些值时,acquireToken将失败,因此它将触发某些事件。Tl:DR;构造函数是哑的,它需要一个URL,而不需要一个具有客户端凭据的URL。但是,有趣的是,如果您提供了相对URL(例如“/signin oidc”)或null,它将无法获取令牌,因此它用于某些内容。。。