Authenticate to Azure API App using ADAL(使用 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 应用进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 ADAL 对 Azure API 应用进行身份验证
基础教程推荐
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 如果条件可以为空 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 将数据集转换为列表 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
