我有一个 MVC 应用程序需要访问受 Azure AD 身份验证保护的 Azure 中的私有 API 应用程序。所以我需要获取 Azure AD 承载令牌,将其转换为Zumo-Auth
令牌并使用它来访问 API 应用程序。
我正在阅读本教程,一切正常,直到我需要从authContext
. 这是代码片段:
var authContext = new AuthenticationContext(
"https://login.microsoftonline.com/MyADDomain.onmicrosoft.com");
ClientCredential credential = new ClientCredential(
"04472E33-2638-FAKE-GUID-F826AF4928DB",
"OMYAPIKEY1x3BLAHEMMEHEHEHEHEeYSOMETHINGRc=");
// Get the AAD token.
var appIdUri =
"https://MyAppGateway-814485545465FAKE4d5a4532cd.azurewebsites.net/login/aad";
//var appIdUri = "https://MyADDomain.onmicrosoft.com/MyAppName";
//var appIdUri = "https://MyADDomain.onmicrosoft.com/";
//var appIdUri = "https://graph.windows.net";
AuthenticationResult result =
authContext.AcquireToken(appIdUri, credential); // <-- can't get the token from AD
// downhill from here
var aadToken = new JObject();
aadToken["access_token"] = result.AccessToken;
var appServiceClient = new AppServiceClient(
"https://MyAppGateway-814485545465FAKE4d5a4532cd.azurewebsites.net/");
// Send the AAD token to the gateway and get a Zumo token
var appServiceUser = await appServiceClient.LoginAsync("aad", aadToken);
与 的行authContext.AcquireToken(appIdUri, credential)
是引起麻烦的行。
如果appIdUri
我给https://MyAppGateway-814485545465FAKE4d5a4532cd.azurewebsites.net/login/aad
,我得到例外:
400: AdalServiceException: AADSTS50001: Resource ' https://MyAppGateway-814485545465FAKE4d5a4532cd.azurewebsites.net/login/aad ' 没有为该帐户注册。
但是这条确切的行在Reply Url
AD 应用程序的列表中
当我尝试使用https://MyADDomain.onmicrosoft.com/MyAppName
或收到不同的异常消息时:https://MyADDomain.onmicrosoft.com/
appIdUri
400:AdalServiceException:AADSTS50105:应用程序“04472E33-2638-FAKE-GUID-F826AF4928DB”未分配给应用程序“ https://MyADDomain.onmicrosoft.com/MyAppName ”的角色
或者
400:AdalServiceException:AADSTS50105:应用程序“04472E33-2638-FAKE-GUID-F826AF4928DB”未分配给应用程序“ https://MyADDomain.onmicrosoft.com/ ”的角色
在这两种情况下,我都App ID URI
将 AD 应用程序中的设置为“ https://MyADDomain.onmicrosoft.com/MyAppName ”或“ https://MyADDomain.onmicrosoft.com/ ”。以及列表中的两个名称Reply URL
。
最终,经过足够多的尝试,我将https://graph.windows.net
as放入appIdUri
并取回了不记名令牌。但是令牌的有效期是过去的(大约过去 1 分钟)。所以我对此无能为力。并401-Unauthenticated
在尝试使用令牌登录 API 应用程序时得到。
我错过了什么?