【问题标题】:Api-App Authentication from web-app来自 web-app 的 Api-App 身份验证
【发布时间】:2015-06-03 15:22:07
【问题描述】:
我在 Azure 上托管了一个 Api-App。我有另一个现有的 JavaScript Web 应用程序客户端。在网络应用程序客户端中,我使用外部登录提供程序,如 goolge 和 Facebook 登录并存储各自的访问令牌。
阅读有关如何使用Azure AD或Facebook对Api-App进行身份验证的文章后,我了解到在调用Api-App服务时,我只需将'x-zumo-auth'及其对应的值添加到请求头中这会变魔术。
现在我的问题是如何在调用 Api-App 服务时重用已在我的网络应用客户端中获取的访问令牌,而无需再次单独调用 http://[gatewayurl]/login/[providername]?
【问题讨论】:
标签:
facebook
authentication
azure
web-applications
azure-api-apps
【解决方案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 应用程序的分步教程,请参阅Call an Azure API app from a web app client authenticated by Azure Active Directory。