无法从Azure AD获取承载令牌以与API App一起使用

问题描述 投票:5回答:1

我有一个MVC应用程序需要访问Azure中受Azure AD身份验证保护的私有API应用程序。所以我需要获取Azure AD不记名令牌,将其转移到Zumo-Auth令牌并使用它来访问API应用程序。

我正在通过this tutorial,一切正常,直到我需要从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列表中

Reply URI in Azure AD Application

当我尝试使用https://MyADDomain.onmicrosoft.com/MyAppNamehttps://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/'的角色

在这两种情况下,我都将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

我错过了什么?

c# azure azure-active-directory azure-ad-graph-api azure-api-apps
1个回答
7
投票

我已经开始按照你提到的教程:Call an Azure API app from a web app client authenticated by Azure Active Directory

  1. 创建返回联系人数据的Azure API Api
  2. 将API应用程序部署到Azure App Service。
  3. 使用Azure Active Directory保护API App。

然后我能够检索令牌,正如您在下面的演示中看到的那样,我的代码与您的代码没有什么不同,除了它使用更高版本的使用using Microsoft.IdentityModel.Clients.ActiveDirectoryAsync库。

Get access token from 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();
    }
}

Constants

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应用程序的Gateway刀片中检索此内容。

这些是适合我的。

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";
}

Final decoded 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"
}.

注意:这是一个带有一次性资源组的一次性活动目录帐户中的一次性应用程序,因此显示我的安全凭据不是问题。

Diagram just to be sure :)

Connecting the dots with Azure ADD

© www.soinside.com 2019 - 2024. All rights reserved.