【问题标题】:Authenticate to Azure API App using ADAL使用 ADAL 对 Azure API 应用进行身份验证
【发布时间】:2015-08-09 23:03:08
【问题描述】:

我有一个标记为“公共(经过身份验证)”的 Azure API 应用程序,并在关联网关中设置了 Azure Active Directory 身份,详见Protect an API App

然后我在同一个 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 应用程序,手动输入了一个 x-zumo-auth header 我进入 Chrome,然后它就可以工作了,但是我使用 ADAL 获得的令牌没有。我还尝试了sample code 中描述的浏览器表单,它可以工作但没有给我刷新令牌。

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

【问题讨论】:

  • 我也有同样的问题。使用 ADAL 收到的 accessToken 总是给我一个 403:Forbidden 响应。我已经尝试过 Windows 控制台 .NET 客户端和 Cordova 客户端。两者结果相同。当 API 应用程序是公共匿名但 403 并经过公共身份验证时没有问题。你在这个问题上有什么进展吗?
  • @Mark 我决定延迟使用 api 网关,直到它得到更好的支持并改用 owin openid 和承载身份验证。这意味着身份验证逻辑又回到了 api 应用程序中,但它可以工作。

标签: c# authentication azure azure-active-directory azure-api-apps


【解决方案1】:

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

授权:承载ThisIsTheAccessTokenYouRecievedFromADAL

【讨论】:

  • 这对我来说适用于其他 Web API(Web 应用程序),但我从 API 应用程序中得到了 403 禁止。
  • 此时,您需要使用 x-zumo-auth 标头来执行此操作。这里有关于如何做到这一点的信息:azure.microsoft.com/en-us/documentation/articles/…。全面的 OAuth2 支持即将推出,但我承认我不知道此功能的 ETA。
【解决方案2】:

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

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

【讨论】:

  • 此示例让我进行身份验证,但强制用户每次重新输入凭据,并且它使用 Windows 窗体,这不是我想要的。如果 API-Apps WebApp 示例通过了身份验证,那可能会为我指明正确的方向。
猜你喜欢
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 2018-11-20
  • 2015-07-27
相关资源
最近更新 更多