以下是检索 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 应用程序。