无法从Azure AD获取承载令牌以与API App一起使用
我有一个MVC应用程序需要访问Azure中受Azure AD身份validation保护的私有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:资源“ https://MyAppGateway-814485545465FAKE4d5a4532cd.azurewebsites.net/login/aad ”未注册该帐户。
但是,此确切行位于AD应用程序中的Reply Url
列表中
当我尝试使用https://MyADDomain.onmicrosoft.com/MyAppName
或https://MyADDomain.onmicrosoft.com/
作为appIdUri
我收到一条不同的exception消息:
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/ ”的角色
在这两种情况下,我都将AD应用程序中的App ID URI
设置为“ https://MyADDomain.onmicrosoft.com/MyAppName ”或“ https://MyADDomain.onmicrosoft.com/ ”。 以及Reply URL
列表中的两个名称。
经过足够多次尝试后,我将https://graph.windows.net
作为appIdUri
并获得了持有者令牌。 但令牌在过去有效期(过去约1分钟)。 所以我对此无能为力。 尝试使用令牌登录API App时获得401-Unauthenticated
。
我错过了什么?
我已经按照您所引用的教程进行操作: 从Azure Active Directoryvalidation的Web应用程序客户端调用Azure API应用程序
- 创建返回联系人数据的Azure API Api
- 将API应用程序部署到Azure App Service。
- 使用Azure Active Directory保护API App。
然后我能够检索令牌,正如您从下面的演示中看到的那样,我的代码与您的代码没有什么不同,除了它使用更高版本的使用Async
using Microsoft.IdentityModel.Clients.ActiveDirectory
库。
从AAD获取访问令牌
class Program { static void Main(string[] args) { var authContext = new AuthenticationContext(Constants.AUTHORITY); var credential = new ClientCredential(Constants.CLIENT_ID, Constants.CLIENT_SECRET); var result = (AuthenticationResult)authContext .AcquireTokenAsync(Constants.API_ID_URL, credential) .Result; var token = result.AccessToken; Console.WriteLine(token.ToString()); Console.ReadLine(); } }
常量
AUTHORITY
。 第一部分是https://login.microsoftonline.com 。 最后一段是允许的租户 。 我们在portal.azure.com上设置允许的租户,转到我们的应用程序的网关,然后选择设置>身份> Azure Active Directory>允许的租户。 我的租客是bigfontoutlook.onmicrosoft.com。
CLIENT_ID
。 我们从添加到Azure Active Directory的应用程序中检索此客户端ID。 在manage.windowsazure.com> Active Directory>您的目录>应用程序>您的应用程序>配置中找到此信息。 检索完毕后,我们必须将其添加到客户端ID字段中的Gateway的Azure Active Directory设置中。
CLIENT_SECRET
。 我们在检索客户端ID的相同位置创建/检索此内容。
API_ID_URL
。 我们通过选择设置>身份> Azure Active Directory>应用URL,在我们的Web API应用的网关刀片中检索此内容。
这些是适合我的。
class Constants { public const string AUTHORITY = "https://login.microsoftonline.com/bigfontoutlook.onmicrosoft.com/"; public const string CLIENT_ID = "0d7dce06-c3e3-441f-89a7-f828e210ff6d"; public const string CLIENT_SECRET = "AtRMr+Rijrgod4b9Q34i/UILldyJ2VO6n2jswkcVNDs="; public const string API_ID_URL = "https://mvp201514929cfaaf694.azurewebsites.net/login/aad"; }
最终解码的JWT
这是解码的JWT访问令牌包含的内容。
{ typ: "JWT", alg: "RS256", x5t: "MnC_VZcATfM5pOYiJHMba9goEKY", kid: "MnC_VZcATfM5pOYiJHMba9goEKY" }. { aud: "https://mvp201514929cfc350148cfa5c9b24a7daaf694.azurewebsites.net/login/aad", iss: "https://sts.windows.net/0252f597-5d7e-4722-bafa-0b26f37dc14f/", iat: 1442346927, nbf: 1442346927, exp: 1442350827, ver: "1.0", tid: "0252f597-5d7e-4722-bafa-0b26f37dc14f", oid: "5a6f33eb-b622-4996-8a6a-600dce355389", sub: "5a6f33eb-b622-4996-8a6a-600dce355389", idp: "https://sts.windows.net/0252f597-5d7e-4722-bafa-0b26f37dc14f/", appid: "0d7dce06-c3e3-441f-89a7-f828e210ff6d", appidacr: "1" }.
注意:这是一个带有一次性资源组的一次性活动目录帐户中的一次性应用程序,因此显示我的安全凭据不是问题。
图只是为了确定:)