首页 > 解决方案 > Azure AAD - 本地服务的应用程序代理

问题描述

我一直在尝试使用应用程序代理从 Azure 中的另一个 Api 调用本地 Api。我已经按照下面列出的几个示例进行了操作,结果相似。我可以成功获取访问令牌,但是当我通过应用代理对本地 Api 进行 http 调用时,即使我添加了带有承载令牌的授权标头,我总是会被重定向到登录页面。有人对我可能缺少的东西有想法吗?

https://blogs.technet.microsoft.com/applicationproxyblog/2018/06/27/securing-api-access-beyond-intranet-with-azure-ad-application-proxy/

这是我正在运行的代码,它是从引用的示例中复制而来的。我不是 100% 了解的一件事是 todoListResourceId 和 todoListBaseAddress。我将它们设置为相同的值,即配置的应用程序代理应用程序的“主页 URL”。

AuthenticationContext authContext;
private static string aadInstance = "https://login.microsoftonline.com/{0}";
private static string tenant = "xxx.onmicrosoft.com";
private static string clientId = "my app id";
Uri redirectUri = new Uri("http://replyurl");
private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
private static string todoListResourceId = "https://xxx.msappproxy.net";
private static string todoListBaseAddress = "https://xxx.msappproxy.net";

private async void GetTodoList()
    {
        AuthenticationResult result = null;
        HttpClient httpClient = new HttpClient();
        authContext = new AuthenticationContext(authority);
        result = await authContext.AcquireTokenAsync(todoListResourceId, clientId, redirectUri, new PlatformParameters(PromptBehavior.Auto));
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

        // Call the To Do list service.
        HttpResponseMessage response = await httpClient.GetAsync(todoListBaseAddress + "/api/values/4");

        //MessageBox.Show(response.RequestMessage.ToString());
        string s = await response.Content.ReadAsStringAsync();
        MessageBox.Show(s);
    }

标签: azureadalmsal

解决方案


推荐阅读