下面的以下代码用于验证ADFS 2016中的用户并为cp.APIBaseURL中定义的资源请求访问令牌:
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
var cp = UnityConfig.Container.Resolve<IConfigurationProvider>();
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = cp.ClientId,
MetadataAddress = cp.MetadataAddress,
RedirectUri = cp.RedirectUri,
PostLogoutRedirectUri = cp.PostLogoutRedirectUri,
ResponseType = "code id_token",
Scope = "openid",
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = OnAuthorizationCodeReceived
}
});
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification context)
{
var cp = UnityConfig.Container.Resolve<IConfigurationProvider>();
AuthenticationContext ac = new AuthenticationContext(
configurationProvider.Authority, false,
new InMemoryTokenCache(context.AuthenticationTicket.Identity.Name));
AuthenticationResult ar = await ac.AcquireTokenByAuthorizationCodeAsync(
context.Code, new Uri(cp.RedirectUri),
new ClientCredential(cp.ClientId, cp.ClientSecretKey),
cp.APIBaseURL);
}
我想知道如何更改代码以为不同的API(具有不同的受众)请求第二个访问令牌?
我还可以为我需要的第二个访问令牌指定不同的范围吗?
您可以使用result = await ac.AcquireTokenSilentAsync(resource, clientId);
请求不同资源的访问令牌。有关更多详细信息,请参见here。
已配置的权限下静态配置范围。我还可以为我需要的第二个访问令牌指定不同的范围吗?
否,对于v1(adal)Azure AD应用程序,必须在Azure门户中的API权限