使用 ADAL 对 Azure API 应用进行身份验证 [英] Authenticate to Azure API App using ADAL

查看:31
本文介绍了使用 ADAL 对 Azure API 应用进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标记为公共(经过身份验证)"的 Azure API 应用程序,并在相关网关中设置了 Azure Active Directory 身份,详细信息请参见 保护 API 应用.

I have an Azure API App marked as "Public (authenticated)" and set up an Azure Active Directory identity in the associated gateway as detailed in Protect an API App.

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

I then created a native application in the same Azure Active Directory Tenant and added permission to access the Gateway in the delegated permissions.

使用 ADAL 和以下代码,我能够成功地进行身份验证并获得访问令牌,但我不知道如何使用它来访问我的 API 应用程序.

Using ADAL and the following code, I'm able to successfully authenticate and get an access token, but I can't figure out how to use it to access my API app.

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;
}

我已经手动输入了一个 x-zumo-auth 标头 测试了 API 应用程序,我进入了 Chrome,然后它就可以工作了,但是我使用 ADAL 获得的令牌却没有.我还尝试了他们的 示例代码可以工作,但没有给我刷新令牌.

I've tested the API app manually entering an x-zumo-auth header I get in Chrome and it works then, but not with a token I get using ADAL. I've also tried the browser forms described in their sample code which works but doesn't give me a refresh token.

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

How do I need to set up my authentication code so I can use a TokenCache and ADAL with my API app?

推荐答案

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

You may want to use AppServiceClient to authenticate the user and invoke a protected API App endpoint. Install Microsoft.Azure.AppService SDK (-pre) Nuget package to your client project.

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

You can find more details in the AzureCards samples on GitHub - https://github.com/Azure-Samples/API-Apps-DotNet-AzureCards-Sample

这篇关于使用 ADAL 对 Azure API 应用进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆