繁体   English   中英

控制台应用程序 (.NET Framework) 中的 AAD 登录提示 - 您可以使用 oauth2 *non v2.0* 端点吗?

[英]AAD login prompt in a console app (.NET Framework) - can you use the oauth2 *non v2.0* endpoint?

因此,我创建了一个由 AAD 保护的 Azure Function。

我正在尝试向用户提示,以便我可以获得不记名令牌来调用我的 Azure Function。

我目前正在尝试PublicClientApplication.AcquireTokenAsync方法(来自命名空间Microsoft.Identity.Client ) - 我正在使用 Azure function 的应用程序 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(使用 Fiddler)中删除/v2.0部分时,生成的 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 Function 以接受 v2.0 令牌,但出于好奇,我仍然想知道如何在没有 v2 的情况下使用这个 API。

非常感谢!

旁注:我已经尝试使用authenticationContext.AcquireTokenAsync方法(来自命名空间Microsoft.IdentityModel.Clients.ActiveDirectory )创建一个“客户端”来调用它。
此方法不起作用,因为我的组织不允许这样做 - 身份验证后,我在登录提示中收到此错误:“[我的客户端应用程序]需要访问组织中只有管理员才能授予的资源的权限。请询问管理员在您使用它之前授予此应用程序的权限。”

只需将范围值更改为{client_id/.default} 它看起来像

string[] scopes = new string[] {"client_id_of_yourFunction/.default"};

参考:

/.default scope

更新:

如果要调用 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 -配置 - 没有/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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM