因此,我创建了一个由AAD保护的Azure功能。
我正在尝试向用户提示,以便我可以获取承载令牌来调用我的Azure函数。
我目前正在尝试PublicClientApplication.AcquireTokenAsync
方法(来自名称空间Microsoft.Identity.Client
)-我在其中使用Azure函数的应用程序ID,不需要指定资源,但是我没有得到正确的令牌返回。
这是我到目前为止的代码:
string[] scopes = new string[] { "profile", "email", "openid" };
string ClientId = "[Client ID of Azure Function]";
string Tenant = "[tenant guid]";
string Instance = "https://login.windows.net/";
var _clientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority($"{Instance}{Tenant}")
.WithDefaultRedirectUri()
.Build();
var accounts = _clientApp.GetAccountsAsync().Result;
var authResult = _clientApp.AcquireTokenInteractive(scopes)
.WithAccount(accounts.FirstOrDefault())
.WithPrompt(Prompt.SelectAccount)
.ExecuteAsync().Result;
var x = authResult.AccessToken; //audience for this is the Client id for Microsoft Graph
var y = authResult.IdToken; //audience for this is the Client id for my Azure Function
我注意到它使用了'v2.0'端点:
https://login.microsoftonline.com/[tenantguid]/v2.0/.well-known/openid-configuration
因此,我找回的id_token不起作用。
但是,经过大量的调试和反复试验后,我注意到当我从URL中删除/v2.0
部分(使用Fiddler)时,生成的id_token起作用了!
预期的jwt应该看起来像这样:
{
"aud": "[Client id of my Azure Function]",
"iss": "https://sts.windows.net/[tenant guid]/", //without a /v2.0 at the end
...
}
如何使它不使用v2.0端点?
编辑:我已经修改了我的Azure函数以接受v2.0令牌,但是出于好奇,我仍然想知道如何在不使用v2.0的情况下使用此API。
非常感谢!
旁注:我已经尝试使用authenticationContext.AcquireTokenAsync
方法(来自名称空间Microsoft.IdentityModel.Clients.ActiveDirectory
)创建一个“客户端”来调用它。此方法无效,因为我的组织不允许这样做-身份验证后,我在登录提示中收到此错误:“ [[我的客户端应用]需要访问权限,只有管理员可以授予您组织中的资源。请询问管理员才能授予对此应用程序的权限,然后才能使用它。“
只需将范围值更改为{client_id/.default}
。它看起来像
string[] scopes = new string[] {"client_id_of_yourFunction/.default"};
参考:
更新:
如果要调用v1.0终结点,则需要使用adal(Microsoft.IdentityModel.Clients.ActiveDirectory)而不是msal。
参考:
https://docs.microsoft.com/en-us/rest/api/datacatalog/authenticate-a-client-app
目前,似乎还没有办法将其发送到非v2.0终结点(除了对dll进行热修补以外。)>
我使用DNSpy进行调试,发现“ v2.0”被硬编码到OpenId Discovery Endpoint中(在内部类中)。
[我可以确认,当我更改了硬编码的字符串时(要删除v2.0部分,它将转到非v2.0端点。(https://login.microsoftonline.com/[tenantid]/.well-known/openid-configuration-不带/v2.0)。
请参见此处查看反编译代码:
namespace Microsoft.Identity.Client.Instance
{
// Token: 0x0200022F RID: 559
internal class AadOpenIdConfigurationEndpointManager : IOpenIdConfigurationEndpointManager
{
// Token: 0x060015E8 RID: 5608 RVA: 0x00049C1F File Offset: 0x00047E1F
public AadOpenIdConfigurationEndpointManager(IServiceBundle serviceBundle)
{
this._serviceBundle = serviceBundle;
}
/// <inheritdoc />
// Token: 0x060015E9 RID: 5609 RVA: 0x00049C30 File Offset: 0x00047E30
public async Task<string> ValidateAuthorityAndGetOpenIdDiscoveryEndpointAsync(AuthorityInfo authorityInfo, string userPrincipalName, RequestContext requestContext)
{
Uri uri = new Uri(authorityInfo.CanonicalAuthority);
if (authorityInfo.ValidateAuthority && !KnownMetadataProvider.IsKnownEnvironment(uri.Host))
{
await this._serviceBundle.InstanceDiscoveryManager.GetMetadataEntryAsync(authorityInfo.CanonicalAuthority, requestContext).ConfigureAwait(false);
}
return authorityInfo.CanonicalAuthority + "v2.0/.well-known/openid-configuration";
}
// Token: 0x04000957 RID: 2391
private readonly IServiceBundle _serviceBundle;
}
}