来自网络应用程序的Api-App身份验证

时间:2015-06-03 15:22:08

标签: facebook authentication azure web-applications azure-api-apps

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

在阅读了有关如何使用Azure ADFacebook验证Api-App的文章之后,我了解到在调用Api-App服务时我只需要添加'x-zumo-auth'及其请求标头的相应值,这将是魔术。

现在我的问题是如何在不再单独调用http://[gatewayurl]/login/[providername]的情况下重新调用Api-App服务来重用已在我的网络应用客户端中获取的访问令牌?

1 个答案:

答案 0 :(得分: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的网络应用程序的分步教程,请参阅Call an Azure API app from a web app client authenticated by Azure Active Directory