我有一个标记为公共(经过身份验证)"的 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 应用进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
ASP Core Azure Active Directory 登录使用角色ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登录使用角色)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph 一起使用)
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超