0

我在 Azure 上托管了一个 Api-App。我有另一个现有的 JavaScript Web 应用程序客户端。在网络应用程序客户端中,我使用外部登录提供程序,如 goolge 和 Facebook 登录并存储各自的访问令牌。

在阅读了有关如何使用Azure ADFacebook对 Api-App 进行身份验证的文章后,我了解到,在调用 Api-App 服务时,我只需将'x-zumo-auth'其相应的值添加到请求标头中,这将起到神奇的作用。

现在我的问题是,如何在调用 Api-App 服务时重用已在我的 Web 应用程序客户端中获取的访问令牌,而无需再次单独调用http://[gatewayurl]/login/[providername]

4

1 回答 1

1

以下是检索 Azure AD 令牌并将其交换为 Zumo 令牌的示例代码,无需通过网关登录:

public async Task<AppServiceClient> GetAppServiceClient()
{
    var appServiceClient = new AppServiceClient(GATEWAY_URL);
    string userObjectID = ClaimsPrincipal.Current.FindFirst
        ("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

    var authContext = new AuthenticationContext
        (ConfigHelper.Authority, new TokenDbCache(userObjectID));

    ClientCredential credential = new ClientCredential
        (ConfigHelper.ClientId, ConfigHelper.AppKey);

    // Get the AAD token.
    AuthenticationResult result = authContext.AcquireToken(APP_ID_URI, credential);
    var aadToken = new JObject();
    aadToken["access_token"] = result.AccessToken;

    // Send the AAD token to the gateway and get a Zumo token
    var appServiceUser = await appServiceClient.LoginAsync
        ("aad", aadToken).ConfigureAwait(false);

    return appServiceClient;
}

有关修改和测试使用 AAD 的 Web 应用程序的分步教程,请参阅从通过 Azure Active Directory 身份验证的 Web 应用程序客户端调用 Azure API 应用程序

于 2015-10-14T21:32:15.913 回答