azure - 从 Dynamics 365 插件通过应用程序服务身份验证访问 Function App

标签 azure oauth oauth-2.0 azure-functions dynamics-365

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

此功能受应用服务身份验证保护。

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

enter image description here

然后我从高级模式中获取了生成的客户端 ID 和客户端 key :

enter image description here

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

客户端ID

客户端 secret

资助类型

资源

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

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();

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

我在 Postman 中遇到同样的错误

有什么想法吗?

最佳答案

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

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

您需要修改 Active Directory 身份验证的高级管理模式,将 https://keyvaultaccess.azurewebsites.net 添加到 ALLOWED TOKEN AUDIENCES 或更改 发送获取 access_token 的 token 请求时,将 resource 参数添加到您的 AAD clientID。

Active Directory 身份验证配置:

enter image description here

测试:

enter image description here

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

enter image description here

访问我的 Azure Function 端点:

enter image description here

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

enter image description here

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

关于azure - 从 Dynamics 365 插件通过应用程序服务身份验证访问 Function App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50482199/

相关文章:

java - Twitter4j:未设置 OAuth 消费者 key / secret

oauth - 是否有基本上匿名的 oauth2 提供者(但可能存储信用卡信息)

c# - 无法使用网络凭据

javascript - 使用 oauth2 检索 Google 地方信息 client_id

azure - 应用程序可以使用 AD 身份验证而不是连接字符串 key 来访问 Azure Cosmos

azure - 应用程序洞察 : How to disable SQL Dependency telemetry

authentication - golang中的单点登录认证

javascript - 使用/files_put javascript 将文件上传到 dropBox

c# - Azure Web应用程序突然给出500个错误

azure - 如何根据Azure门户上的显示方式识别Azure虚拟机的驱动器?