【问题标题】:Azure API Apps Access Token (ADAL) not workingAzure API 应用程序访问令牌 (ADAL) 不起作用
【发布时间】:2017-03-25 13:18:21
【问题描述】:

我创建了一个 API(Azure API 应用程序)并使用 Azure Active Directory 启用了身份验证/授权(来自 APP API。应用程序服务已在 AAD 中注册,到目前为止一切正常。

我已按照以下帖子中的步骤生成令牌,但令牌似乎不起作用。

var authContext = new AuthenticationContext("https://login.microsoftonline.com/<guid>/oauth2/authorize");
var credential = new ClientCredential("<clientId>", "<secret_from_aad>");
var result = (AuthenticationResult)authContext.AcquireTokenAsync("http://<api>.azurewebsites.net", credential).Result;
var token = result.AccessToken;

https://msdn.microsoft.com/en-us/library/azure/mt428036.aspx

原始请求如下所示:

GET https://<api>.azurewebsites.net/rest/v1/crm/surveys/all HTTP/1.1
Host: <api>.azurewebsites.net
Connection: close
Accept-Encoding: gzip,deflate
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJ<rest-of-token...>
Host: <api>.azurewebsites.net
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

回复是

HTTP/1.1 401 Unauthorized
Content-Length: 58
Content-Type: text/html
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer realm="<api>.azurewebsites.net"
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=dd32cab21d0ca9541343a77c51d355d0781c0e0a4147a2166ecb955fe9d94a60;Path=/;Domain=<api>.azurewebsites.net
Date: Fri, 11 Nov 2016 11:20:49 GMT
Connection: close

You do not have permission to view this directory or page.

我一直在努力寻找一种无需显示登录页面即可生成令牌的方法,但我不确定这是不是最好的方法(虽然很简单......)。

使用 API 的系统必须能够以编程方式生成令牌并随请求一起发送。

我创建了一个虚拟 AAD、一个虚拟应用服务等并将此代码示例放在一起:

class Program
    {
        static void Main(string[] args)
        {
            string response = HttpRequest();
            Console.ReadLine();
        }

        public static string HttpRequest()
        {
            string serviceURI = "https://apiappdemo.azurewebsites.net/api/values/";

            //Get the access token
            string token = GetToken();

            HttpWebRequest request = System.Net.WebRequest.Create(serviceURI) as System.Net.HttpWebRequest;
            request.KeepAlive = true;
            request.Method = "GET";
            request.ContentLength = 0;
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

            using (HttpWebResponse httpResponse = request.GetResponse() as System.Net.HttpWebResponse) //Failing here... 401
            {
                using (StreamReader reader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }
        }

        public static string GetToken()
        {
            var authContext = new AuthenticationContext("https://login.microsoftonline.com/b37d6c4c-012f-450c-81c7-406b6b584348/oauth2/authorize");
            var credential = new ClientCredential("7ed0dccb-ade7-4a5e-b286-32b66eb929d1", "1bVIoJyMHsbuYsfuJ7or6krbKvWw3kpKfp69jsQuilw=");
            var result = (AuthenticationResult)authContext.AcquireTokenAsync("https://apiappdemo.azurewebsites.net", credential).Result;
            return result.AccessToken;
        }
    }

有什么想法可能是错的吗?

问候, 迈克

【问题讨论】:

    标签: azure authentication active-directory token adal


    【解决方案1】:

    要使令牌适用于受 Azure AD 保护的应用服务,我们需要使用 高级 托管模式(请参阅here)。

    我们将在 Azure AD 中使用两个应用程序 reigster,第一个用于保护应用程序服务。第二个用作客户端来请求资源。我们需要指定允许的令牌受众,即应用程序的应用程序 ID URI(第一个应用程序)。这是一个供您参考的图:

    然后我们可以使用第二个应用程序作为客户端来获取为应用程序服务配置的应用程序的令牌。并确保您在AcquireTokenAsync 方法中指定的资源是预览设置中的audience 配置。

    【讨论】:

    • 不确定我在这里关注你,为什么我需要两个应用程序,另一个应用程序实际上是什么?
    • Azure 中有两个概念。首先是需要保护的资源。其次是需要访问受保护资源的客户端。当您使用应用服务的身份验证/授权功能对其进行配置时,第一个应用代表资源。然后第二个应用代表您使用其客户端 ID、客户端密码和第一个应用的应用 ID URI(将作为令牌中的 aud 声明)获取第一个应用的令牌的客户端。
    【解决方案2】:

    替换这一行:

    authContext.AcquireTokenAsync("https://apiappdemo.azurewebsites.net"
                                       ,credential).Result; 
    

    与:

    authContext.AcquireTokenAsync("7ed0dccb-ade7-4a5e-b286-32b66eb929d1"
                                      ,credential).Result; 
    

    使用您的客户端 ID 作为资源 ID

    【讨论】:

    • 虽然这可能会回答这个问题,但最好解释一下答案的基本部分,以及 OPs 代码可能存在什么问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2018-12-20
    相关资源
    最近更新 更多