使用 OnAuthorizationCodeReceived 检索 Azure GraphAPI AccessToken
Using OnAuthorizationCodeReceived to retrieve Azure GraphAPI AccessToken
我目前正在我的管道中使用代码来缓存使用 Azure AD 的图形 API 的不记名令牌。这段代码是从一个工作的 ASP.NET 4 应用程序移植过来的,但感觉 Core 中新的 OpenIdConnectOptions 应该会让这更容易。我是否可以在 OnAuthorizationCodeReceived 事件中使用更直接的调用,一旦收到代码,该调用将使用 AuthenticationContext 来缓存令牌?这是我当前的代码:
var azureSettings = app.ApplicationServices.GetService<IOptions<AzureSettings>>().Value;
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = azureSettings.ClientId,
ClientSecret = azureSettings.AppKey,
Authority = string.Format(azureSettings.AadInstance, azureSettings.TenantId),
Resource = azureSettings.GraphResourceUri,
ResponseType = OpenIdConnectResponseType.CodeIdToken,
TokenValidationParameters = new TokenValidationParameters
{
RoleClaimType = "roles"
},
Events = new OpenIdConnectEvents()
{
OnAuthorizationCodeReceived = (context) =>
{
string resourceUri = azureSettings.GraphResourceUri;
var authContext = new AuthenticationContext(context.Options.Authority);
var credential = new ClientCredential(context.TokenEndpointRequest.ClientId, context.TokenEndpointRequest.ClientSecret);
var result = authContext.AcquireTokenByAuthorizationCodeAsync(context.TokenEndpointRequest.Code, new Uri(context.TokenEndpointRequest.RedirectUri), credential, resourceUri);
context.HandleCodeRedemption(result.AccessToken, result.IdToken);
}
}
});
上面的代码工作得很好,但感觉我复制了很多代码只是为了提交大部分已经包含在 AuthorizationCodeReceivedContext 中的内容。
有没有我忽略的更简单的方法?
查看 Microsoft.AspNetCore.Authentication.OpenIdConnect 的代码后,我意识到该库与 AuthenticationContext 中的令牌缓存机制断开连接。如果我尝试简化代码,它不会触发缓存机制,这意味着需要在每次请求时检索 Bearer 令牌。
因为我计划使用 TokenCache 来减少对 API 的调用并最终利用我的 Redis 缓存,所以我需要将该代码留在 OnAuthorizationCodeReceived 方法中。
我目前正在我的管道中使用代码来缓存使用 Azure AD 的图形 API 的不记名令牌。这段代码是从一个工作的 ASP.NET 4 应用程序移植过来的,但感觉 Core 中新的 OpenIdConnectOptions 应该会让这更容易。我是否可以在 OnAuthorizationCodeReceived 事件中使用更直接的调用,一旦收到代码,该调用将使用 AuthenticationContext 来缓存令牌?这是我当前的代码:
var azureSettings = app.ApplicationServices.GetService<IOptions<AzureSettings>>().Value;
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = azureSettings.ClientId,
ClientSecret = azureSettings.AppKey,
Authority = string.Format(azureSettings.AadInstance, azureSettings.TenantId),
Resource = azureSettings.GraphResourceUri,
ResponseType = OpenIdConnectResponseType.CodeIdToken,
TokenValidationParameters = new TokenValidationParameters
{
RoleClaimType = "roles"
},
Events = new OpenIdConnectEvents()
{
OnAuthorizationCodeReceived = (context) =>
{
string resourceUri = azureSettings.GraphResourceUri;
var authContext = new AuthenticationContext(context.Options.Authority);
var credential = new ClientCredential(context.TokenEndpointRequest.ClientId, context.TokenEndpointRequest.ClientSecret);
var result = authContext.AcquireTokenByAuthorizationCodeAsync(context.TokenEndpointRequest.Code, new Uri(context.TokenEndpointRequest.RedirectUri), credential, resourceUri);
context.HandleCodeRedemption(result.AccessToken, result.IdToken);
}
}
});
上面的代码工作得很好,但感觉我复制了很多代码只是为了提交大部分已经包含在 AuthorizationCodeReceivedContext 中的内容。
有没有我忽略的更简单的方法?
查看 Microsoft.AspNetCore.Authentication.OpenIdConnect 的代码后,我意识到该库与 AuthenticationContext 中的令牌缓存机制断开连接。如果我尝试简化代码,它不会触发缓存机制,这意味着需要在每次请求时检索 Bearer 令牌。
因为我计划使用 TokenCache 来减少对 API 的调用并最终利用我的 Redis 缓存,所以我需要将该代码留在 OnAuthorizationCodeReceived 方法中。