从 Dynamics 365 插件通过应用服务身份验证访问功能应用

Accessing Function App via App Service Authentication from Dynamics 365 Plugin

我正在尝试通过服务到服务调用从 Dynamics 365 插件访问 Azure 函数:https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-service-to-service

此功能通过应用服务身份验证

受到保护

我创建了一个功能应用程序并启用了应用程序服务身份验证 在平台功能下 -> Authentication/Authorisation。 我启用了 Azure Active Directory 作为 Auth Provider 并将管理模式设置为 Express

然后我从高级模式中得到了生成的客户端 ID 和客户端密码:

显然,这就是为 Azure 函数发出令牌请求所需的全部内容,根据我需要 4 个必需参数的文章:

客户编号

客户端密码

资助类型

资源

我发出以下请求以从 Dynamics 365 插件生成令牌,但收到以下错误:

Invalid Plugin Execution Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: {"error":"invalid_client","error_description":"AADSTS70002: Error validating credentials. AADSTS50012: Invalid client secret is provided.\r\nTrace ID: 06ddda7f-2996-4c9b-ab7e-b685ee933700\r\nCorrelation ID: d582e2f2-91eb-4595-b44b-e95f42f2f071\r\nTimestamp: 2018-05-23 06:30:58Z","error_codes":[70002,50012],"timestamp":"2018-05-23 06:30:58Z","trace_id":"06ddda7f-2996-4c9b-ab7e-b685ee933700","correlation_id":"d582e2f2-91eb-4595-b44b-e95f42f2f071"}-The remote server returned an error: (401) Unauthorized.

我的代码是:

         var tokenendpoint = "https://login.microsoftonline.com/de194c13-5ff7-4085-91c3-ac06fb869f28/oauth2/token";
         var reqstring = "client_id=" + Uri.EscapeDataString("5f315431-e4da-4f68-be77-4e257b1b9295");
         reqstring += "&client_secret=" + Uri.EscapeDataString("/oK7nh8pl+LImBxjm+L7WsQdyILErysOdjpzvA9g9JA=");
         reqstring += "&resource=" + Uri.EscapeUriString("https://keyvaultaccess.azurewebsites.net");
         reqstring += "&grant_type=client_credentials";

         //Token request
         WebRequest req = WebRequest.Create(tokenendpoint);
         req.ContentType = "application/x-www-form-urlencoded";
         req.Method = "POST";
         byte[] bytes = System.Text.Encoding.ASCII.GetBytes(reqstring);
         req.ContentLength = bytes.Length;
         System.IO.Stream os = req.GetRequestStream();
         os.Write(bytes, 0, bytes.Length);
         os.Close();

         //Token response
         HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
         StreamReader tokenreader = new StreamReader(resp.GetResponseStream());
         string responseBody = tokenreader.ReadToEnd();

我已确保我拥有正确的客户端密钥,并且还对其进行了编码,因为我在某处读到“+”和“/”是不好的。

我在 Postman 中遇到同样的错误

有什么想法吗??

reqstring += "&resource=" + Uri.EscapeUriString("https://keyvaultaccess.azurewebsites.net");

由于您将 resource 参数设置为 https://keyvaultaccess.azurewebsites.net,我假设您已经设置了 AAD 应用程序的 App ID URI(clientId 等于 5f315431-xxxxxxx-4e257b1b9295) 到 https://keyvaultaccess.azurewebsites.net。我假设您可以检索 access_token,但是当使用 access_token 访问您的 azure 函数端点时,您得到了 401 状态代码。

您需要修改 Active Directory 身份验证的高级管理模式,将 https://keyvaultaccess.azurewebsites.net 添加到 ALLOWED TOKEN AUDIENCES 或将 resource 参数更改为您的 AAD access_token.

发送token请求时的clientID

Active Directory 身份验证配置:

测试:

对 JWT 令牌进行编码以检查 aud 属性:

访问我的 Azure 函数端点:

注意:您需要按如下方式处理函数的授权级别:

如果您还启用了函数级身份验证,则发送到 Azure 函数的请求需要在查询字符串中包含相关代码参数,或者将 header x-functions-key 设置为您的函数键的值,或者您可以将授权级别设置为匿名。