繁体   English   中英

使用ADAL对Azure API App进行身份验证

[英]Authenticate to Azure API App using ADAL

我有一个标记为“公共(经过身份验证)”的Azure API应用程序,并在关联网关中设置Azure Active Directory标识,如“ 保护API应用程序”中所述

然后,我在同一个Azure Active Directory租户中创建了一个本机应用程序,并添加了在委派权限中访问网关的权限。

使用ADAL和以下代码,我能够成功验证并获取访问令牌,但我无法弄清楚如何使用它来访问我的API应用程序。

string Tenant = "[xxx].onmicrosoft.com";
string Authority = "https://login.microsoftonline.com/" + Tenant;
string GatewayLoginUrl = "https://[gateway].azurewebsites.net/login/aad";
string ClientId = "[native client id]";
Uri RedirectUri = new Uri("[native client redirect url]");

async Task<string> GetTokenAsync()
{
  AuthenticationContext context = new AuthenticationContext(Authority);
  PlatformParameters platformParams = new PlatformParameters(PromptBehavior.Auto, null);
  AuthenticationResult result = await context.AcquireTokenAsync(GatewayLoginUrl, ClientId, RedirectUri, platformParams);

  return result.AccessToken;
}

我已经测试了API应用程序手动输入我在Chrome中获得的x-zumo-auth header然后它可以正常工作,但是没有我使用ADAL的令牌。 我也尝试了他们的示例代码中描述的浏览器表单,但它没有给我一个刷新令牌。

如何设置我的身份验证代码,以便在我的API应用程序中使用TokenCache和ADAL?

通常,在调用web api时,会在Authorization标头中传递访问令牌:

授权:Bearer ThisIsTheAccessTokenYouRecievedFromADAL

您可能希望使用AppServiceClient对用户进行身份验证并调用受保护的API App端点。 将Microsoft.Azure.AppService SDK(-pre)Nuget包安装到您的客户端项目。

您可以在GitHub上的AzureCards示例中找到更多详细信息 - https://github.com/Azure-Samples/API-Apps-DotNet-AzureCards-Sample

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM