简体   繁体   English

在 C# 客户端中使用 AAD 身份验证调用 Azure function

[英]Calling Azure function in C# client with AAD authentication

I've created a sample Azure Function, nothing complex, just a simple Hello, World.我创建了一个示例 Azure Function,没什么复杂的,只是一个简单的 Hello, World。 An App has been registered with AAD and the function has been configured for AAD Authentication and Authorization Level is set to Anonymous.应用程序已在 AAD 中注册,并且已为 AAD 身份验证配置 function,并且授权级别设置为匿名。 Using a browser I can navigate to the function URL, be asked to login and get results as expected.使用浏览器,我可以导航到 function URL,要求登录并按预期获得结果。 When using a C# client, after getting a token (which is the same as the one used in the browser) I get 401 Unauthorized result.使用 C# 客户端时,在获取令牌(与浏览器中使用的令牌相同)后,我得到 401 Unauthorized 结果。 I've also tried Postman with the same results;我也尝试过 Postman ,结果相同; 401 "You do not have permission to view this directory or page." 401 “您无权查看此目录或页面。”

var clientCredential = new ClientCredential("16c17039-xxxx-4514-xxxx-fc68a97fxxxx", "00000009q_XwxPP]oyDo8UqZfAsxxxx");
AuthenticationContext context = new AuthenticationContext("https://login.microsoftonline.com/xxxxx.onmicrosoft.com", false);
AuthenticationResult authResult = await context.AcquireTokenAsync(
    "16c17039-xxxx-4514-xxxx-fc68a97fxxxx",
    clientCredential);

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
var response = await client.GetAsync("https://xxxx.azurewebsites.net/api/Sample?code=xxxxx");
response.EnsureSuccessStatusCode();

Obviously I'm missing some configuration but can't seem to find what the issue is.显然我缺少一些配置,但似乎无法找到问题所在。

I didn't reproduce your issue on my side.我没有在我这边重现您的问题。 Here are my steps for your reference.这是我的步骤供您参考。

1.Create a function with http trigger 1.用http触发器创建一个function

在此处输入图像描述

2.Enable App Service Authentication by using Express mode. 2.使用快速模式启用应用服务身份验证。

在此处输入图像描述

3.Call the function url using C# 3.调用 function url 使用 C#

static void Main(string[] args)
        {
            var clientCredential = new ClientCredential("{app_client_id}", "{app_client_secret}");
            AuthenticationContext context = new AuthenticationContext("https://login.microsoftonline.com/xx.onmicrosoft.com", false);
            AuthenticationResult authResult = context.AcquireTokenAsync(
                "{app_client_id}",
                clientCredential).Result;

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            var response = client.GetAsync("https://tonytestad.azurewebsites.net/api/HttpTrigger1?code=LoyOi4C&name=123").Result;
            response.EnsureSuccessStatusCode();
        }

4.See the result in vs 4.在vs中查看结果

在此处输入图像描述

The only difference in the code is that I used Result to get the result.代码中唯一的区别是我使用Result来获取结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM