[英]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"};
參考:
更新:
如果要調用 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.