简体   繁体   English

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

[英]Authenticate to Azure API App using ADAL

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 API应用程序,并在关联网关中设置Azure Active Directory标识,如“ 保护API应用程序”中所述

I then created a native application in the same Azure Active Directory Tenant and added permission to access the Gateway in the delegated permissions. 然后,我在同一个Azure Active Directory租户中创建了一个本机应用程序,并添加了在委派权限中访问网关的权限。

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. 使用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;
}

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. 我已经测试了API应用程序手动输入我在Chrome中获得的x-zumo-auth header然后它可以正常工作,但是没有我使用ADAL的令牌。 I've also tried the browser forms described in their sample code which works but doesn't give me a refresh token. 我也尝试了他们的示例代码中描述的浏览器表单,但它没有给我一个刷新令牌。

How do I need to set up my authentication code so I can use a TokenCache and ADAL with my API app? 如何设置我的身份验证代码,以便在我的API应用程序中使用TokenCache和ADAL?

Generally you pass the access token in the Authorization header when when calling a web api: 通常,在调用web api时,会在Authorization标头中传递访问令牌:

Authorization: Bearer ThisIsTheAccessTokenYouRecievedFromADAL 授权:Bearer ThisIsTheAccessTokenYouRecievedFromADAL

You may want to use AppServiceClient to authenticate the user and invoke a protected API App endpoint. 您可能希望使用AppServiceClient对用户进行身份验证并调用受保护的API App端点。 Install Microsoft.Azure.AppService SDK (-pre) Nuget package to your client project. 将Microsoft.Azure.AppService SDK(-pre)Nuget包安装到您的客户端项目。

You can find more details in the AzureCards samples on GitHub - https://github.com/Azure-Samples/API-Apps-DotNet-AzureCards-Sample 您可以在GitHub上的AzureCards示例中找到更多详细信息 - https://github.com/Azure-Samples/API-Apps-DotNet-AzureCards-Sample

暂无
暂无

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

相关问题 是否使用令牌使用ADAL对Azure API应用进行身份验证 - Authenticate to Azure API App using ADAL using Token or Not 如何在 WPF 应用程序中使用 Z3A580F142203677F0BC30898 AZFZ 验证 web api - How to authenticate web api in WPF app using Azure AD 使用ADAL.js获得的令牌针对Azure移动服务应用进行身份验证 - Authenticate against an Azure Mobile Service App with ADAL.js acquired token 使用身份框架进行用户身份验证的 Azure Active Directory 身份验证和现有应用程序。 使用 ADAL 或 Web Api - Azure Active Directory Authentication with and Existing App which is using Identity framework for user authentication. Using ADAL or Web Api 如何使用Azure代理AD和ADAL在SharePoint上进行身份验证? - How to Authenticate on SharePoint On Prem with Azure Proxy AD and ADAL? Azure Logic App 使用 MSI 向 WebApp 进行身份验证 - Azure Logic App authenticate with WebApp using MSI Azure-使用MSI替代方案对控制台应用程序进行身份验证 - Azure - authenticate a console app using alternative to MSI 使用ClientID使用ADAL v3对Dynamics 365进行身份验证 - Authenticate to Dynamics 365 using ADAL v3 using ClientID 使用ADAL.net来验证Azure AD凭据的自定义Web应用程序登录页面 - Custom web app login page using ADAL.net to validate Azure AD credentials 如何使用应用程序ID和应用程序密钥授权/身份验证API请求 - How to Authorize/Authenticate an API request using an app id and app key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM